使用数组元素在MongoDB中获取特定文档
要获取特定文档,请在MongoDB中使用点表示法find()。让我们创建一个包含文档的集合-
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea3ea9b04263e90dac943e5")
}
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Samsung"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea3eaa404263e90dac943e6")
}
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"OnePlus"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea3eacc04263e90dac943e7")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo672.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] }
{ "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Samsung" } ] }
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }以下是获取特定文档的查询-
> db.demo672.find({"Brand.Name":"OnePlus"});这将产生以下输出-
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短