我们可以在MongoDB中搜索对象数组吗?
是的,要搜索对象数组,请在MongoDB中使用$unwindaggregate()。要匹配,请使用$match。让我们创建一个包含文档的集合-
> db.demo623.insertOne(
... {
... _id:1,
... details:[
... {
... Name:"Chris"
... },
... {
... DueDate:new ISODate("2020-01-10")
... },
... {
... CountryName:"US"
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 1 }在find()方法的帮助下显示集合中的所有文档-
> db.demo623.find().pretty();
这将产生以下输出-
{
"_id" : 1,
"details" : [
{
"Name" : "Chris"
},
{
"DueDate" : ISODate("2020-01-10T00:00:00Z")
},
{
"CountryName" : "US"
}
]
}以下是在MongoDB中搜索对象数组的查询-
> db.demo623.aggregate({$unwind: "$details"},
... {$match: {"details.Name":"Chris"}},
... {$project: {"details.Name": 1}})这将产生以下输出-
{ "_id" : 1, "details" : { "Name" : "Chris" } }热门推荐
10 获奖评语简短祝福语大全
11 婚礼酒祝福语简短大气
12 满月祝福语长辈妈妈简短
13 给妈妈祝福语简短内容
14 学生国庆简短的祝福语
15 爸妈金婚文案祝福语简短
16 国庆节日简短祝福语
17 美丽祝福语老师文案简短
18 迎元旦平安祝福语简短