collection.find() 总是返回 MongoDB 的所有字段?
您可以使用以下语法返回特定字段。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 - 如您所知,如果您只使用 tofind()那么它会返回所有字段。
查询如下 -
> 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 的所有字段? 的全部内容, 来源链接: utcz.com/z/317429.html