数组元素上的mongocollection过滤器

时间:2017-08-02 14:24:39

标签: mongodb aggregation-framework mongo-java

我有一份具有以下结构的文件

    {
        "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}
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

将您的$group阶段更改为使用$first而不是$push,以避免重复。

Document groupResult = new Document("$group", new Document("_id", "$id").append("array", new Document("$push", "$profile.array")).append("sub", new Document("$first", "$sub")));

或者,您可以重构代码以使用$filter替换$unwind + $match + $group

Document idQuery = new Document("id", new Document("$in", ids));
Document idMatch = new Document("$match", idQuery);
Document filterQuery = new Document("$gte", Arrays.asList("$$profilearray.score", 2));
Document filterArray = new Document("$filter", new Document("input", "$profile.array").append("as", "profilearray").append("cond", filterQuery));
Document projectArray = new Document("$project", new Document("id",1).append("profile.array", filterArray).append("sub", 1));
List<Document> pipeLineList = new ArrayList<Document>();
pipeLineList.add(idMatch);
pipeLineList.add(projectArray);