如何使用'$ match'MongoDB的聚合运算符来匹配嵌入文档的id?

时间:2022-06-21 16:11:01

Scenario: Consider the document present in the MongoDB in collection named 'MyCollection'

场景:考虑名为“MyCollection”的集合中MongoDB中存在的文档

       {
         "_id" : ObjectId("512bc95fe835e68f199c8686"),
         "author": "dave",
         "score" : 80,
         "USER" : {
                   "UserID": "Test1",
                   "UserName": "ABCD"
                  }
       },
       { "_id" : ObjectId("512bc962e835e68f199c8687"),
         "author" : "dave",
         "score" : 85,
         "USER" : {
                   "UserID": "Test2",
                   "UserName": "XYZ"
                  }
       },
       ...

I know the UserID and want to fetch based on that.

我知道UserID并想要基于此获取。

Issue: I tried the following code with Node.js + MongoDB-native driver:

问题:我使用Node.js + MongoDB本机驱动程序尝试了以下代码:

   db.Collection('MyCollection', function (err, collection) {
        if (err) return console.error(err); 
        collection.aggregate([
                         { $match: { '$USER.UserID': 'Test2'} }, 
                        {$group: {
                            _id: '$_id' 
                        }
                    },
                        {
                            $project: {
                                _id: 1 
                            }
                        }
                      ], function (err, doc) {
                          if (err) return console.error(err);
                          console.dir(doc); 
                      });
           });

But its not working as expected.

但它没有按预期工作。

Question: Can anyone know how to do the same with $match operator in MongoDB query?

问:任何人都知道如何在MongoDB查询中使用$ match运算符执行相同的操作吗?


Update: I am not getting any error. But the object will be blank i.e. []

1 个解决方案

#1


1  

I tried in the shell and your $match statement is wrong - trying in the shell

我尝试在shell中你的$ match语句是错误的 - 尝试在shell中

> db.MyCollection.find()
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "USER" : { "UserID" : "Test1", "UserName" : "ABCD" } }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "USER" : { "UserID" : "Test2", "UserName" : "XYZ" } }
> db.MyCollection.aggregate([{$match: {"$USER.UserID": "Test2"}}])
{ "result" : [ ], "ok" : 1 }
> db.MyCollection.aggregate([{$match: {"USER.UserID": "Test2"}}])
{
    "result" : [
        {
            "_id" : ObjectId("512bc962e835e68f199c8687"),
            "author" : "dave",
            "score" : 85,
            "USER" : {
                "UserID" : "Test2",
                "UserName" : "XYZ"
            }
        }
    ],
    "ok" : 1
}

So the full aggregation would be:

所以完整的聚合将是:

db.MyCollection.aggregate([
  {$match: {"USER.UserID": "Test2"}},
  {$group: {"_id": "$_id"}},
  {$project: {"_id": 1}}
])

(You don't need the extra $project as you only project _id in the $group but equally as _id is unique you should just have the $project and remove the $group)

(你不需要额外的$项目,因为你只在$ group中预测_id,但同样地,_id是唯一的,你应该只有$ project并删除$ group)

#1


1  

I tried in the shell and your $match statement is wrong - trying in the shell

我尝试在shell中你的$ match语句是错误的 - 尝试在shell中

> db.MyCollection.find()
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "USER" : { "UserID" : "Test1", "UserName" : "ABCD" } }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "USER" : { "UserID" : "Test2", "UserName" : "XYZ" } }
> db.MyCollection.aggregate([{$match: {"$USER.UserID": "Test2"}}])
{ "result" : [ ], "ok" : 1 }
> db.MyCollection.aggregate([{$match: {"USER.UserID": "Test2"}}])
{
    "result" : [
        {
            "_id" : ObjectId("512bc962e835e68f199c8687"),
            "author" : "dave",
            "score" : 85,
            "USER" : {
                "UserID" : "Test2",
                "UserName" : "XYZ"
            }
        }
    ],
    "ok" : 1
}

So the full aggregation would be:

所以完整的聚合将是:

db.MyCollection.aggregate([
  {$match: {"USER.UserID": "Test2"}},
  {$group: {"_id": "$_id"}},
  {$project: {"_id": 1}}
])

(You don't need the extra $project as you only project _id in the $group but equally as _id is unique you should just have the $project and remove the $group)

(你不需要额外的$项目,因为你只在$ group中预测_id,但同样地,_id是唯一的,你应该只有$ project并删除$ group)