Mongo DB:如何选择嵌套数组计数>的项目0
数据库接近5GB。我有这样的文档:
{
_id: ..
user: "a"
hobbies: [{
_id: ..
name: football
},
{
_id: ..
name: beer
}
...
]
}
我想返回拥有超过 0 个“爱好”的用户 我试过了
db.collection.find({"hobbies" : { > : 0}}).limit(10)
,它占用了所有内存,但没有结果。
- 如何进行此选择?
- 以及如何仅返回: id、name、count ?
- 用c#官方驱动怎么做?
蒂亚
附近我发现: “为hande类别大小添加新字段。这是mongo世界的常见做法。” 这是真的吗?
The database is near 5GB. I have documents like:
{
_id: ..
user: "a"
hobbies: [{
_id: ..
name: football
},
{
_id: ..
name: beer
}
...
]
}
I want to return users who have more then 0 "hobbies"
I've tried
db.collection.find({"hobbies" : { > : 0}}).limit(10)
and it takes all RAM and no result.
- How to do conduct this select?
- And how to return only: id, name, count ?
- How to do it with c# official driver?
TIA
P.S.
near i've found:
"Add new field to hande category size. It's a usual practice in mongo world."
is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这种特定情况下,您可以使用列表索引来解决您的问题:
db.collection.find({"hobbies.0" : {$exists : true}}).limit(10)
这只是确保第 0 个元素存在。您可以执行相同的操作,通过检查范围末尾是否存在元素来确保列表的长度小于 n 或介于 x 和 y 之间。
In this specific case, you can use list indexing to solve your problem:
db.collection.find({"hobbies.0" : {$exists : true}}).limit(10)
This just makes sure a 0th element exists. You can do the same to make sure the list is shorter than n or between x and y in length by checking the existing of elements at the ends of the range.
您是否尝试过使用
hobbies.length
。我还没有测试过这个,但我相信这是在 mongodb 中查询数组范围的正确方法Have you tried using
hobbies.length
. i haven't tested this, but i believe this is the right way to query the range of the array in mongodb您可以(某种程度上)使用逻辑
$not
通过$size
运算符检查数组长度范围:You can (sort of) check for a range of array lengths with the
$size
operator using a logical$not
:这话有些道理。
根据手册
http://www.mongodb.org/display/ DOCS/Advanced+Queries#AdvancedQueries-%24size
因此,您可以检查数组大小是否为 0,但不能检查“大于 0”之类的内容
That's somewhat true.
According to the manual
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size
So you can check for array size 0, but not for things like 'larger than 0'
之前的问题解释了如何处理数组计数问题。尽管在您的情况下,如果 ZERO 确实是您想要测试的唯一值,则您可以在数组为空时将其设置为 null 并将选项设置为不序列化它,然后您可以测试该字段是否存在。当您想要向用户添加爱好时,请记住测试 null 并创建数组。
对于#2,如果您添加了计数字段,则可以轻松地从数据库中选择您想要返回的字段并包含计数字段。
Earlier questions explain how to handle the array count issue. Although in your case if ZERO really is the only value you want to test for, you could set the array to null when it's empty and set the option to not serialize it, then you can test for the existence of that field. Remember to test for null and to create the array when you want to add a hobby to a user.
For #2, provided you added the count field it's easy to select the fields you want back from the database and include the count field.
如果您只需要查找零个爱好,并且没有为零个爱好的人设置爱好键,请使用 EXISTS 标志。
添加“爱好”索引以增强性能:
db.collection.find( { 爱好 : { $exists : true } } );
但是,如果爱好为零的人有空数组,爱好 1 的人有一个包含 1 个元素的数组,则使用此通用解决方案:
维护一个名为“hcount”(爱好计数)的变量,并在任何更新中始终将其设置为等于爱好数组的大小。
字段“hcount”上的索引
然后,您可以执行如下查询:
3 - 根据 @JohnPs 的回答,“$size”对于此目的也是一个很好的运算符。
http://www.mongodb.org/display/DOCS/Advanced+查询#AdvancedQueries-%24size
if you need to find only zero hobbies, and if the hobbies key is not set for someone with zero hobbies , use EXISTS flag.
Add an index on "hobbies" for performance enhancement :
db.collection.find( { hobbies : { $exists : true } } );
However, if the person with zero hobbies has empty array, and person with 1 hobby has an array with 1 element, then use this generic solution :
Maintain a variable called "hcount" ( hobby count), and always set it equal to size of hobbies array in any update.
Index on the field "hcount"
Then, you can do a query like :
3 - From @JohnPs answer, "$size" is also a good operator for this purpose.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size