_idがサブドキュメントの一部として配列にあるドキュメントを照合するMongoDBクエリ?
> db.demo568.insertOne({ _id: 101, details: [ {id : 101 }, { id:103 } ] });
{ "acknowledged" : true, "insertedId" : 101 } find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo568.find();
これにより、次の出力が生成されます-
{ "_id" : 101, "details" : [ { "id" : 101 }, { "id" : 103 } ] }
Following is the query to create second collection:
> db.demo569.insertOne({ _id: 101, details: "John" })
{ "acknowledged" : true, "insertedId" : 101 }
> db.demo569.insertOne({ _id: 102, details: "Chris" })
{ "acknowledged" : true, "insertedId" : 102 }
> db.demo569.insertOne({ _id: 103, details: "David" })
{ "acknowledged" : true, "insertedId" : 103 } find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo569.find();
これにより、次の出力が生成されます-
{ "_id" : 101, "details" : "John" }
{ "_id" : 102, "details" : "Chris" }
{ "_id" : 103, "details" : "David" } 以下は、_idがサブドキュメントの一部として配列内にあるドキュメントを照合するためのクエリです-
> db.demo569.find({ '_id': { '$in': db.demo568.distinct('details.id', {'_id': 101}) }}) これにより、次の出力が生成されます-
{ "_id" : 101, "details" : "John" }
{ "_id" : 103, "details" : "David" } -
MongoDBに埋め込まれたドキュメントの配列をクエリし、別のドキュメントをプッシュしますか?
このために、更新と一緒に$pushを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo573.insertOne( ... { ... '_id' :101, ... 'SearchInformation' : [ ... { ... 'Site'
-
配列フィールドを含むドキュメントに一致するMongoDBクエリ
配列フィールドを含むドキュメントを照合するには、$elemMatch演算子を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo592.insertOne( ... { ... "id":101, ... "details" : [ ... { "Name" : "Chris", "Value