0
我有具有以下結構的mongocollection濾波器
{
"id" : "abcdefg",
"sub" : {"field1" : "value1", "field3" : "value3"},
"profile" : {
"array" : [
{"score" : 1},
{"score" : 2},
{"score" : 3}
]
}
}
我想在profile.score.score過濾文檔,我使用下面
Document idQuery = new Document("id", new Document("$in", ids));
// ids is an array of id
Document idMatch = new Document("$match", idQuery);
Document projectArray = new Document("$project", new Document("id",1).append("profile.array", 1).append("sub", 1));
Document unwindArray = new Document("$unwind", "$profile.array");
Document filterQuery = new Document("profile.array.score", new Document("$gte", 2));
Document filterMatch = new Document("$match", filterQuery);
Document groupResult = new Document("$group", new Document("_id", "$id").append("array", new Document("$push", "$profile.array")).append("sub", new Document("$push", "$sub")));
List<Document> pipeLineList = new ArrayList<Document>();
pipeLineList.add(idMatch);
pipeLineList.add(projectArray);
pipeLineList.add(unwindArray);
pipeLineList.add(filterMatch);
pipeLineList.add(groupResult);
AggregateIterable<Document> result = companyProfiles.aggregate(pipeLineList);
通過以上做時,我具有
{
"id" : "abcdefg",
"sub" : [
{"field1" : "value1", "field3" : "value3"},
{"field1" : "value1", "field3" : "value3"}
]
"profile" : {
"array" : [
{"score" : 2},
{"score" : 3}
]
}
}
子字段插入兩次作爲陣列中,這是不我想要的元件的結果。我應該怎麼做纔能有正確的結果
{
"id" : "abcdefg",
"sub" : {"field1" : "value1", "field3" : "value3"},
"profile" : {
"array" : [
{"score" : 2},
{"score" : 3}
]
}
}