与聚合匹配 MongoDB 中的所有值
要匹配 MongoDB 中的所有值,请在聚合中使用 $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 }
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP