您可以使用下列語法從collection.find()傳回特定欄位。
情況1 - 語法如下 -
db.yourCollectionName.find({},{"yourFieldName":1}).pretty();
上面的欄位名稱設為 1 表示它將只傳回該欄位。如果設為 0,它將傳回除設定為 0 的欄位之外的所有欄位。
情況 2 - 語法如下 -
db.yourCollectionName.find({},{"yourFieldName":0}).pretty();
為了理解上述語法,讓我們用 document 建立一個集合。使用文件建立集合的查詢如下 -
> db.returnFieldInFindDemo.insertOne({"StudentName":"John","StudentAge":23,"TechnicalSubject":["MongoDB","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebfe72f684a30fbdfd566") } > db.returnFieldInFindDemo.insertOne({"StudentName":"Mike","StudentAge":24,"TechnicalSubject":["Java","Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebffd2f684a30fbdfd567") } > db.returnFieldInFindDemo.insertOne({"StudentName":"Sam","StudentAge":22,"TechnicalSubject":["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ec00f2f684a30fbdfd568") } > db.returnFieldInFindDemo.insertOne({"StudentName":"Carol","StudentAge":20,"TechnicalSubject":["DataStructure","Algorithm"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ec0292f684a30fbdfd569") }
借助 find() 方法顯示集合中的所有文件。查詢如下 -
> db.returnFieldInFindDemo.find().pretty();
以下是輸出 -
{ "_id" : ObjectId("5c8ebfe72f684a30fbdfd566"), "StudentName" : "John", "StudentAge" : 23, "TechnicalSubject" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5c8ebffd2f684a30fbdfd567"), "StudentName" : "Mike", "StudentAge" : 24, "TechnicalSubject" : [ "Java", "Python" ] } { "_id" : ObjectId("5c8ec00f2f684a30fbdfd568"), "StudentName" : "Sam", "StudentAge" : 22, "TechnicalSubject" : [ "C", "C++" ] } { "_id" : ObjectId("5c8ec0292f684a30fbdfd569"), "StudentName" : "Carol", "StudentAge" : 20, "TechnicalSubject" : [ "DataStructure", "Algorithm" ] }
情況 1 - 這是您將傳回特定欄位的查詢。
查詢如下 -
> db.returnFieldInFindDemo.find().pretty();
輸出:
{ "_id" : ObjectId("5c8ebfe72f684a30fbdfd566"), "StudentName" : "John", "StudentAge" : 23, "TechnicalSubject" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5c8ebffd2f684a30fbdfd567"), "StudentName" : "Mike", "StudentAge" : 24, "TechnicalSubject" : [ "Java", "Python" ] } { "_id" : ObjectId("5c8ec00f2f684a30fbdfd568"), "StudentName" : "Sam", "StudentAge" : 22, "TechnicalSubject" : [ "C", "C++" ] } { "_id" : ObjectId("5c8ec0292f684a30fbdfd569"), "StudentName" : "Carol", "StudentAge" : 20, "TechnicalSubject" : [ "DataStructure", "Algorithm" ] }
看看上面的範例輸出,我已將「TechnicalSubject」欄位初始化為 1。這意味著它將僅從所有文件中返回“TechnicalSubject”欄位。
情況2
情況2
strong> - 在第二種情況下,如果將「TechnicalSubject」欄位設為0,則表示您將取得除“TechnicalSubject”之外的所有欄位。查詢如下-
> db.retunFieldInFindDemo.find({},{"TechnicalSubject":0}).pretty();
以下是輸出:
{ "_id" : ObjectId("5c8ebfe72f684a30fbdfd566"), "StudentName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5c8ebffd2f684a30fbdfd567"), "StudentName" : "Mike", "StudentAge" : 24 } { "_id" : ObjectId("5c8ec00f2f684a30fbdfd568"), "StudentName" : "Sam", "StudentAge" : 22 } { "_id" : ObjectId("5c8ec0292f684a30fbdfd569"), "StudentName" : "Carol", "StudentAge" : 20 }
情況3 - 如您所知,如果您只使用find() 那麼它會傳回所有欄位。
查詢如下 -
> db.retunFieldInFindDemo.find();
以下是輸出:
{ "_id" : ObjectId("5c8ebfe72f684a30fbdfd566"), "StudentName" : "John", "StudentAge" : 23, "TechnicalSubject" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5c8ebffd2f684a30fbdfd567"), "StudentName" : "Mike", "StudentAge" : 24, "TechnicalSubject" : [ "Java", "Python" ] } { "_id" : ObjectId("5c8ec00f2f684a30fbdfd568"), "StudentName" : "Sam", "StudentAge" : 22, "TechnicalSubject" : [ "C", "C++" ] } { "_id" : ObjectId("5c8ec0292f684a30fbdfd569"), "StudentName" : "Carol", "StudentAge" : 20, "TechnicalSubject" : [ "DataStructure", "Algorithm" ] }
以上是collection.find() 總是傳回 MongoDB 的所有欄位?的詳細內容。更多資訊請關注PHP中文網其他相關文章!