如何在节点js中使用mongodb查询从多重数组中获取记录?

时间:2016-03-12 10:59:48

标签: node.js mongodb

我是mongodb的新用户,我想获取匹配条件的记录,其中u_id等于特定的u_id。我很困惑在哪里/什么条件应该用于获取特定记录。

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

1 个答案:

答案 0 :(得分:0)

您想使用$elemMatch

$formMapper
        ->add('invitees' , 'sonata_type_collection',
            array(
                'required' => true,
                'by_reference' => false,
                'btn_add'=>true,

            ),array(
                'edit' => 'inline',
                'allow_delete' => true,
                'allow_add'=>true,
                'admin_code'      => 'sonata.admin.invitees'
            )
        )
       ;