MongoDBを使用して埋め込みドキュメントの配列でクエリをフィルタリングしますか?
これには、MongoDBでaggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう-
> db.demo736.insertOne(
... {
... "_id": "101",
... "details1": [
... {
... "details2": [
... {
... "details3": {
... "Name": "John"
... }
... }
... ]
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : "101" }
> db.demo736.insertOne(
... {
... "_id": "102",
... "details1": [
... {
... "details2": [
... {
... "details3": {
... "Name": "Robert"
... }
... }
... ]
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : "102" } find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo736.find();
これにより、次の出力が生成されます-
{ "_id" : "101", "details1" : [ { "details2" : [ { "details3" : { "Name" : "John" } } ] } ] }
{ "_id" : "102", "details1" : [ { "details2" : [ { "details3" : { "Name" : "Robert" } } ] } ] } 以下は、MongoDBの埋め込みドキュメント(3レベル)の配列をフィルタリングするためのクエリです-
> db.demo736.aggregate([
... {
... "$unwind": "$details1"
... },
... {
... "$unwind": "$details1.details2"
... },
... {
... "$match": {
... "details1.details2.details3.Name": "Robert"
... }
... },
... {
... $project: {
... _id: 0,
... Name: "$details1.details2.details3",
... }
... }
... ]) これにより、次の出力が生成されます-
{ "Name" : { "Name" : "Robert" } } -
配列要素を使用してMongoDBで特定のドキュメントをフェッチします
特定のドキュメントをフェッチするには、MongoDB find()でドット表記を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac9
-
MongoDB-埋め込まれたドキュメントをクエリしますか?
MongoDBに埋め込まれたドキュメントをクエリするには、aggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo705.insertOne( ... { ... _id:101, ... "Information": ... [ ... { ... &nbs