為此,只需在 MongoDB 中使用點表示法和 find() 即可。讓我們建立一個包含文件的集合-
> db.demo465.insertOne( ... { ... id: 101, ... details: [{ ... Name: "Chris", ... Info: { ... Subject: "MongoDB", ... Marks:67 ... } ... }, { ... Name: "David", ... Info: { ... Subject: "MySQL", ... Marks:78 ... } ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e80421bb0f3fa88e2279061") } > > db.demo465.insertOne( ... { ... id: 102, ... details: [{ ... Name: "Bob", ... Info: { ... Subject: "Java", ... Marks:45 ... } ... }, { ... Name: "Carol", ... Info: { ... Subject: "C", ... Marks:67 ... } ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e80421cb0f3fa88e2279062") }
借助find() 方法顯示集合中的所有文件-
> db.demo465.find();
這將產生以下輸出-
{ "_id" : ObjectId("5e80421bb0f3fa88e2279061"), "id" : 101, "details" : [ { "Name" : "Chris", "Info" : { "Subject" : "MongoDB", "Marks" : 67 } }, { "Name" : "David", "Info" : { "Subject" : "MySQL", "Marks" : 78 } } ] } { "_id" : ObjectId("5e80421cb0f3fa88e2279062"), "id" : 102, "details" : [ { "Name" : "Bob", "Info" : { "Subject" : "Java", "Marks" : 45 } }, { "Name" : "Carol", "Info" : { "Subject" : "C", "Marks" : 67 } } ] }
以下是取得數組中包含另一個文件的所有文件的查詢-
> db.demo465.find({"details.Name":"Bob"});
這將產生以下輸出-
{ "_id" : ObjectId("5e80421cb0f3fa88e2279062"), "id" : 102, "details" : [ { "Name" : "Bob", "Info" : { "Subject" : "Java", "Marks" : 45 } }, { "Name" : "Carol", "Info" : { "Subject" : "C", "Marks" : 67 } } ] }
以上是如何使用 MongoDB 取得數組中包含另一個文件的所有文件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!