简体   繁体   English

如何在Node JS中使用mongodb查询从多维数组中获取记录?

[英]How to fetch record from multi-di array using mongodb query in node js?

I am new to mongodb where i want to fetch record that matches condition for u_id is equal to particular u_id. 我是mongodb的新手,在这里我想获取与u_id条件匹配的记录等于特定的u_id。 I am confusing where/what condition should be applied there for fetching that particular record. 我感到困惑的是应该在哪里/什么条件应用那里获取特定记录。

connection.model.find( {'abc.sar.0.u_id': new ObjectID( '56e3e5b6fb33e53f119051f2' )} , function ( err, result ) { });

[
        {
          "_id": "56e15c49908fb5af0238f05c",
          "abc": {
            "sar": [
              {
                "u_id": "56e18c33909aa6f214e17dec",
                "cd": null,
                "_id": "56e18c33909aa6f214e17df0"
              },
              {
                "u_id": "56e18c4a909aa6f214e17df1",
                "cd": null,
                "_id": "56e18c4a909aa6f214e17df5"
              },
              {
                "u_id": "56e18f6a909aa6f214e17df6",
                "cd": null,
                "_id": "56e18f6a909aa6f214e17dfa"
              },
              {
                "u_id": "56e3c477a8b75d720c73e5a7",
                "cd": null,
                "_id": "56e3c477a8b75d720c73e5ab"
              },
              {
                "u_id": "56e3e5b6fb33e53f119051f2",
                "cd": null,
                "_id": "56e3e5b6fb33e53f119051f6"
              },
              {
                "u_id": "56e3e60efb33e53f119051f7",
                "cd": null,
                "_id": "56e3e60efb33e53f119051fb"
              },
              {
                "u_id": "56e3eaedc0e2dba711805f0e",
                "cd": null,
                "_id": "56e3eaedc0e2dba711805f12"
              },
              {
                "u_id": "56e3eb85f81fe30d12507709",
                "cd": null,
                "_id": "56e3eb86f81fe30d1250770d"
              }
            ]
          }
        }
      ]

You want to use $elemMatch . 您要使用$ elemMatch

connection.model.find({
    'abc.sar' : { 
        $elemMatch : {'u_id': new ObjectID( '56e3e5b6fb33e53f119051f2' )}
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM