使用MongoDB透過特定值查詢具有欄位名的文件?
要根據特定值透過欄位名查詢文件,您可以使用 $exists 運算子。讓我們建立一個包含文件的集合
> db.findByFieldName.insertOne( { "Client":{ "ClientDetails":{ "ClientName":"Larry", "ClientAge":29 }, "ClientProjectDetails":{ "ProjectName":"Online Book Store", "TeamSize":10, "TechnologyUsed":"Spring Boot" } } } );
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9e93b2d628fa4220163b64")
}
> db.findByFieldName.insertOne({
... " Client":{
... " ClientDetails":{
... " ClientName":"Chris",
... " ClientAge":27
... },
... "ClientEducationDetails":{
... " isEducated":true,
... "CollegeName":"M.I.T."
...
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9e9421d628fa4220163b65")
}以下是使用 find() 方法從集合中顯示所有文件的查詢
> db.findByFieldName.find().pretty();
這將產生以下輸出
{
"_id" : ObjectId("5c9e93b2d628fa4220163b64"),
"Client" : {
"ClientDetails" : {
"ClientName" : "Larry",
"ClientAge" : 29
},
"ClientProjectDetails" : {
"ProjectName" : "Online Book Store",
"TeamSize" : 10,
"TechnologyUsed" : "Spring Boot"
}
}
}
{
"_id" : ObjectId("5c9e9421d628fa4220163b65"),
"Client" : {
"ClientDetails" : {
"ClientName" : "Chris",
"ClientAge" : 27
},
"ClientEducationDetails" : {
"isEducated" : true,
"CollegeName" : "M.I.T."
}
}
}以下是根據欄位名稱查詢文件的查詢
> db.findByFieldName.find({"Client.ClientProjectDetails":{$exists: true}}).pretty();這將產生以下輸出
{
"_id" : ObjectId("5c9e93b2d628fa4220163b64"),
"Client" : {
"ClientDetails" : {
"ClientName" : "Larry",
"ClientAge" : 29
},
"ClientProjectDetails" : {
"ProjectName" : "Online Book Store",
"TeamSize" : 10,
"TechnologyUsed" : "Spring Boot"
}
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP