【问题标题】:Node.JS + MongoDB aggregation Find array in array from DataBase in MongoDBNode.JS + MongoDB聚合从MongoDB中的DataBase中查找数组中的数组
【发布时间】:2015-08-14 12:48:00
【问题描述】:

我在 MongoDB 中有一个模型,其中包含一个数组(类别),其中包含一个数组(图片)。 衬衫模型如下所示:

{ _id: 100, category: [ { name: 'CatName', _id: 101, picture: [Object] } ] }

我的图片数组看起来像这样:

 [ { path: 'p1', _id: 102 },
   { path: 'p2', _id: 103 } ] }

我通过了 3 个变量

  1. 主数组ID 100 (var1)
  2. 类别名称是 CatName (var2)
  3. 图片id是102 (var3)

我想获取一个如下所示的数组:

{ _id: 100, category: [ { name: 'CatName', _id: 101,
                          picture: [ { path: 'p1', _id: 102 }]  
                        } ]
}

我试过的是这样的:

Shirt.find(   {_id: var1} ,  
{category: { $and:[ { name: var2 } , { picture: {$elemMatch: {_id: var3}}}  ] }}  )
.exec(function(err, product) {
    console.log('Result  ' + product );
    res.jsonp(product);
});

但我收到的结果是未定义的第一个代码

我尝试的第二个代码:

Shirt.find( {_id: var1} ,
            {category: {$elemMatch: { name: var2,picture: {$elemMatch: {_id: var3} }}} }  )

第二个代码的结果过滤 var1var2 的数组 但它包含整个图片数组,这意味着它不过滤 var3

  1. 找到我想要的东西的正确代码是什么?
  2. 这是购物网站数据库的正确方法还是您有更好的建议?
  3. 我是否正确应用了父子数据库?

谢谢!

【问题讨论】:

  • 使用“picture._id”代替$elemMatch
  • 它不起作用。而且我不认为 DOT 是在 mongodb 查找操作中定义的

标签: arrays node.js mongodb


【解决方案1】:

以下 mongo 查询将为您提供预期的结果:

db.collection.find({
    "category": {
    $elemMatch: {
        "name": "CatName",
        "picture": {
            $elemMatch: {
                "_id": 102
            }
        }
    }
    }
})

需要转换成node.js格式。

以下格式可能会对您有所帮助。 未测试

collection.find({
    _id: "" // add your match Id here} ,  
        {
            category: {
                "$elemMatch": {
                    "name": "CatName",
                    "picture": {
                        $elemMatch: {
                            "_id": 102
                        }
                    }
                }
            }
        }
    })
    .exec(function(err, product) {
    console.log('Result  ' + product);
    res.jsonp(product);
    });

修改后的问题

您应该使用mongo aggregation 来获取所需的值。查询将如下所示 -

db.collection.aggregate({
    $unwind: '$category'
}, {
    $unwind: '$category.picture'
}, {
    $match: {
    _id: 100, // add here your var1 variable instead of 100
    'category.name': 'CatName', // add here your var2 variable instead of 'CatName'
    'category._id': 102, //add your match id here if any otherwise remove this condition
    'category.picture._id': 102 //add your var3 instead of 102
    }
}, {
    $group: {
    _id: '$category.name',
    'name': {
        $first: '$category.name'
    },
    'picture': {
        '$push': '$category.picture'
    }
    }
})

【讨论】:

  • 我只是用你的代码更新我的问题但是我收到了整个图片数组
  • 答案是根据您编辑前的问题。所以你只需要从图片数组中匹配字段,对吧?
  • 如果您看到我尝试的第二个代码与您的完全一样并且结果包括整个图片数组但我希望它只返回一张图片
  • 那 100 不是 Category._id 你能不能用 var1 , var2 和 var3 再写一遍 那 100 是 Shirt._id
  • @SaeedNode.js 您可以在匹配中添加任何条件。我只是将其添加为示例。你可以改变它
猜你喜欢
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 2019-09-28
  • 2019-02-02
  • 1970-01-01
  • 2020-11-27
相关资源
最近更新 更多