您可以使用以下语法从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" ] }