MongoDB 中的 collection.find() 方法是否始终返回所有字段?
您可以使用以下语法从 collection.find() 中返回特定字段。
情况 1 − 语法如下所示:
db.yourCollectionName.find({},{"yourFieldName":1}).pretty();
上述字段名称设置为 1 表示它将仅返回该字段。如果设置为 0,则它将返回除设置为 0 的字段之外的所有字段。
情况 2 − 语法如下所示:
db.yourCollectionName.find({},{"yourFieldName":0}).pretty();
为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下所示:
> 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 − 在第二种情况下,如果将字段“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" ] }
广告