要匹配MongoDB中的所有值,请在$aggregation中将$match与$and一起使用。让我们创建一个包含文档的集合-
> db.demo574.insertOne(
... {
... "details1": {
... "details2": {
... "dueDate": new ISODate("2020-01-10"),
... "Name": "Chris",
...
... "UserInformation": {
... "Name": "John",
... "Marks": 78
... },
... CountryName:"US"
... },
... id:101
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e9167f3581e9acd78b427f6")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo574.find();
这将产生以下输出-
{
"_id" : ObjectId("5e9167f3581e9acd78b427f6"), "details1" :
{ "details2" : { "dueDate" : ISODate("2020-01-10T00:00:00Z"), "Name" : "Chris", "UserInformation" :
{ "Name" : "John", "Marks" : 78 }, "CountryName" : "US" }, "id" : 101 }
}以下是用于聚合和匹配的查询-
> db.demo574.aggregate({
... $match: {
... $and: [
... {"details1.id": 101},
... {"details1.details2.UserInformation.Name": 'John'},
... {"details1.details2.Name": 'Chris'}
... ]
... }
... }
... );这将产生以下输出-
{
"_id" : ObjectId("5e9167f3581e9acd78b427f6"), "details1" :
{ "details2" : { "dueDate" : ISODate("2020-01-10T00:00:00Z"), "Name" : "Chris", "UserInformation" :
{ "Name" : "John", "Marks" : 78 }, "CountryName" : "US" }, "id" : 101 }
}