使用点表示法在MongoDB查询中选择字段?
为此,请在MongoDB中使用点表示法进行字段选择find()
。让我们创建一个包含文档的集合-
> db.demo302.insertOne({"Id":101,"details":[{"Name":"Chris",Age:21,"Subject":"MySQL"}]});{
"acknowledged" : true,
"insertedId" : ObjectId("5e4d746f5d93261e4bc9ea52")
}
> db.demo302.insertOne({"Id":102,"details":[{"Name":"Bob",Age:23,"Subject":"MongoDB"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e4d74815d93261e4bc9ea53")
}
> db.demo302.insertOne({"Id":103,"details":[{"Name":"David",Age:20,"Subject":"Java"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e4d74955d93261e4bc9ea54")
}
在find()
方法的帮助下显示集合中的所有文档-
> db.demo302.find();
这将产生以下输出-
{ "_id" : ObjectId("5e4d746f5d93261e4bc9ea52"), "Id" : 101, "details" : [ { "Name" : "Chris", "Age" : 21, "Subject" : "MySQL" } ] }{ "_id" : ObjectId("5e4d74815d93261e4bc9ea53"), "Id" : 102, "details" : [ { "Name" : "Bob", "Age" : 23, "Subject" : "MongoDB" } ] }
{ "_id" : ObjectId("5e4d74955d93261e4bc9ea54"), "Id" : 103, "details" : [ { "Name" : "David", "Age" : 20, "Subject" : "Java" } ] }
以下是使用点表示法的字段选择查询-
>db.demo302.find({"details.Subject":"MongoDB"},{"details.Name":0,"details.Age":0,_id:0,Id:0});
这将产生以下输出-
{ "details" : [ { "Subject" : "MongoDB" } ] }
以上是 使用点表示法在MongoDB查询中选择字段? 的全部内容, 来源链接: utcz.com/z/322289.html