MongoDB-如何查询集合中的嵌套项目? [英] MongoDB - how to query for a nested item inside a collection?
本文介绍了MongoDB-如何查询集合中的嵌套项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些看起来像这样的数据:
I have some data that looks like this:
[
{
"_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
"advertisers" : [
{
"created_at" : ISODate("2011-07-26T21:02:19Z"),
"category" : "Infinity Pro Spin Air Brush",
"updated_at" : ISODate("2011-07-26T21:02:19Z"),
"lowered_name" : "conair",
"twitter_name" : "",
"facebook_page_url" : "",
"website_url" : "",
"user_ids" : [ ],
"blog_url" : "",
},
,我当时在想这样的查询会给出广告客户的ID:
and I was thinking that a query like this would give the id of the advertiser:
var start = new Date(2011, 1, 1);
> var end = new Date(2011, 12, 12);
> db.agencies.find( { "created_at" : {$gte : start , $lt : end} } , { _id : 1 , program_ids : 1 , advertisers { name : 1 } } ).limit(1).toArray();
但是我的查询无效.知道如何将嵌套元素内的字段添加到要获取的字段列表中吗?
But my query didn't work. Any idea how I can add the fields inside the nested elements to my list of fields I want to get?
谢谢!
推荐答案
使用点表示法(例如advertisers.name
)查询和检索嵌套对象中的字段:
Use dot notation (e.g. advertisers.name
) to query and retrieve fields from nested objects:
db.agencies.find({
"advertisers.created_at": {
$gte: start,
$lt: end
}
},
{
_id: 1,
program_ids: 1,
"advertisers.name": 1
}
}).limit(1).toArray();
这篇关于MongoDB-如何查询集合中的嵌套项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文