Mongodb 按MongoTemplate聚合中的id列表筛选对象列表

Mongodb 按MongoTemplate聚合中的id列表筛选对象列表,mongodb,mongodb-query,aggregation-framework,mongotemplate,Mongodb,Mongodb Query,Aggregation Framework,Mongotemplate,我有一部分操作如下 mongoTemplate.aggregate( newAggregation(unwind("catalogItems"), group("catalogItems._id").addToSet("catalogItems.modifierId").as("modIds").addToSet( new BasicDBObject("_id", "$recipe._id").append("name",

我有一部分操作如下

 mongoTemplate.aggregate(
        newAggregation(unwind("catalogItems"),
            group("catalogItems._id").addToSet("catalogItems.modifierId").as("modIds").addToSet(
                new BasicDBObject("_id", "$recipe._id").append("name", "$recipe.name")
                    .append("menuId", "$recipe.menuId").append("status", "$recipe.status")
                    .append("modifierList",
        new BasicDBObject("modifiers", new BasicDBObject("id", new BasicDBObject("$in", "$modIds")))))
                .as("recipeList")),
        "table", Ingredient.class).getMappedResults();
这里我要做的是分配修饰符,它包含$modIds中的id

modifierList - the property I'm going to assign the result with filtered modifiers
modifiers - input list that contains all elements
$modIds - modifier ids got by previous projection
当我尝试这一点时,我得到以下错误

Expression $in takes exactly 2 arguments. 1 were passed in.' on server
我觉得我的语法不好。但我找不到符合我要求的工作示例。你知道怎么做吗?

试试这个:

Aggregation agg = newAggregation(
    unwind("catalogItems"), 
    match(Criteria.where("catalogItems.name").regex("^" + name, "i")),
    group("catalogItems._id", "catalogItems.name","catalogItems.status")
        .addToSet("catalogItems.modifierId").as("modIds")
        .addToSet(new Document("_id", new Document("$toString","$recipe._id"))
                .append("name", "$recipe.name")
                .append("menuId", "$recipe.menuId")
                .append("modifiers","$modifiers")).as("recipeList"), 
    project()
        .and("_id._id").as("_id")
        .and("_id.name").as("name")
        .and("_id.status").as("status")
        .and(op -> new Document("$map", 
                new Document("input", "$recipeList")
                    .append("as", "recipe")
                    .append("in", new Document("id","$$recipe._id")
                            .append("name", "$$recipe.name")
                            .append("status", "$$recipe.status")
                            .append("menuId", "null")
                            .append("modifiers", new Document("$map", 
                                new Document("input", new Document("$filter", 
                                    new Document("input", "$$recipe.modifiers")
                                        .append("as", "modif")
                                        .append("cond", new Document("$in", Arrays.asList("$$modif._id", "$modIds"))))
                                    ).append("as", "mod")
                                .append("in", new Document("id", "$$mod._id")
                                        .append("recipeId", "$$mod.recipeId")
                                        .append("displayName", "$$mod.displayName")
                                        .append("status", "$$mod.status"))))
                                ))).as("recipeList"));

mongoTemplate.aggregate(agg, "table", Ingredient.class).getMappedResults();

您需要显示使用完整查询。似乎要在文档的字段中搜索
$?在这种情况下,您应该使用I have updated the codeNow I get the error
$In需要一个数组作为第二个参数,found:missing'在服务器上
@Sandeepa,这意味着您的
目录项
数组为空。共享一个示例数据,我将修复示例数据过长的问题。我在这里与您聊天时分享了我的查询和示例数据。这样你会有一个清晰的想法。非常感谢您的帮助:)@Sandeepa再次检查我的答案