MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

Aggregationを使用して、MongoDBのすべての値を照合します


MongoDBのすべての値を一致させるには、Aggregationで$andとともに$matchを使用します。ドキュメントを使用してコレクションを作成しましょう-

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

  1. すべてのドキュメントで個別の値をカウントするためのMongoDBクエリ?

    これには、MongoDBでaggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo718.insertOne( ...    { ...       "id":101, ...       "details": ...       { ...          "OtherDetails": ["Chris&q

  2. MongoDBの特定の文字列でフィールドのすべての値を更新しますか?

    すべての値を更新するには、update()をmulti:trueとともに使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo720.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOn