[英]Querying Nested List Existence in Mongo
I have a document in Mongo that is structured in the following way: 我在Mongo中有一个文档,其结构如下:
{
"_id" : ObjectId("4eea7237d0ba3a04f20008fb"),
"code" : "b2677c2809c844cc9d7e3e4ff8d95b46",
"city_id" : 4,
"datetime" : ISODate("2011-12-13T18:41:44.062Z"),
"plays" : [
{
"play_id" : 717224,
"clicks" : [ ],
"order" : 1,
"mysql_id" : 145
}
I want to query for docs whose plays.clicks attribute is a non-empty list. 我想查询其plays.clicks属性是非空列表的文档。 I've tried exists with no luck.
我试过没有运气的存在。 I thought that something like this might work:
我认为这样的事情可能有用:
db.collection.find({plays.clicks.0: {$exists:true}})
But I believe that this would return only docs whose first element in the plays array contains a non-empty clicks list. 但我相信这只会返回播放数组中第一个元素包含非空单击列表的文档。
Any thought on how I might get this done? 我是怎么想到这件事的?
Thanks 谢谢
db.collection.find({plays.clicks.0: {$exists:true}})
is the right syntax, however as plays
is a list the query will match any document that has clicks in plays
. 是正确的语法,但是,因为
plays
是一个列表,查询将匹配plays
中具有点击次数的任何文档 。 There is no way to retrieve a subset of an Array for subelements in this way[1]. 没有办法以这种方式为子元素检索数组的子集[1]。 There is a ticket for sub / virtual collections[2]
有一个子/虚拟收藏票[2]
[1] http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements [1] http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements
[2] https://jira.mongodb.org/browse/SERVER-828 [2] https://jira.mongodb.org/browse/SERVER-828
Save the size of the list as a separate attribute (eg num_plays
). 将列表的大小保存为单独的属性(例如
num_plays
)。 Then you can query for documents where num_plays
is greater than 0
: 然后,您可以查询
num_plays
大于0
文档:
Haven't tested, but I think the query you want is 尚未测试,但我认为您想要的查询是
{ "plays.clicks" : { "$size" : 0 } }
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.