與聚合匹配 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