Mongo DB:查找嵌套数组值 [英] Mongo DB: Find nested array value

查看:66
本文介绍了Mongo DB:查找嵌套数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node.js 并尝试查找包含匹配productId"的数组在与匹配销售"相同的文档中.我在 mongo 文档中没有找到太多关于此的内容.

I am working with node.js and trying to find array containing matching "productId" in the same document as matching "sale". I didn't find much in the mongo documentation regarding this.

{"_id":
    {"$oid":"601baf0e5c307422c0fa958c"},
    "sale":"Test Sale",
    "products":[
      [
        ["productId","value1","value2"],
        ["productId","value1","value2"]
      ],
      [
        ["productId","value1","value2"],
        ["productId","value1","value2"]
      ],
      [
        ["productId","value1","value2"],
        ["productId","value1","value2"]
      ]
    ],
    "collectionProducts":["id","id","id"]
}

推荐答案

playground

db.collection.find({
  "sale": "Test Sale",
  products: {
    "$elemMatch": {//As you have nested arrays
      "$elemMatch": {
        "$elemMatch": {
          "$in": [
            "abc"
          ]
        }
      }
    }
  }
})

最好有更好的架构.

这篇关于Mongo DB:查找嵌套数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆