在MongoD中使用数组值获取特定文档
limit()与一起使用toArray()。该toArray()方法返回一个数组,其中包含游标中的所有文档。让我们创建一个包含文档的集合-> db.demo482.insertOne({_id:1,"StudentInformation":[{"Name":"Chris","Age":21}]});
{ "acknowledged" : true, "insertedId" : 1 }
> db.demo482.insertOne({_id:2,"StudentInformation":[{"Name":"Bob","Age":23}]});
{ "acknowledged" : true, "insertedId" : 2 }
> db.demo482.insertOne({_id:3,"StudentInformation":[{"Name":"David","Age":20}]});
{ "acknowledged" : true, "insertedId" : 3 }在find()方法的帮助下显示集合中的所有文档-
> db.demo482.find();
这将产生以下输出-
{ "_id" : 1, "StudentInformation" : [ { "Name" : "Chris", "Age" : 21 } ] }
{ "_id" : 2, "StudentInformation" : [ { "Name" : "Bob", "Age" : 23 } ] }
{ "_id" : 3, "StudentInformation" : [ { "Name" : "David", "Age" : 20 } ] }以下是使用limit()-获取特定文档的查询-
> db.demo482.find({}).limit(2).toArray();这将产生以下输出-
[
{
"_id" : 1,
"StudentInformation" : [
{
"Name" : "Chris",
"Age" : 21
}
]
},
{
"_id" : 2,
"StudentInformation" : [
{
"Name" : "Bob",
"Age" : 23
}
]
}
]热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短