Mongo DB:如何选择嵌套数组计数>的项目0

发布于 2024-12-06 21:20:56 字数 599 浏览 0 评论 0原文

数据库接近5GB。我有这样的文档:

{
    _id: ..
    user: "a"
    hobbies: [{
        _id: ..
        name: football
    },
    {
        _id: ..
        name: beer
    }
    ...
    ]
}

我想返回拥有超过 0 个“爱好”的用户 我试过了

db.collection.find({"hobbies" : { &gt : 0}}).limit(10)

,它占用了所有内存,但没有结果。

  1. 如何进行此选择?
  2. 以及如何仅返回: id、name、count ?
  3. 用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.

  1. How to do conduct this select?
  2. And how to return only: id, name, count ?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不再让梦枯萎 2024-12-13 21:20:56

在这种特定情况下,您可以使用列表索引来解决您的问题:

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.

神回复 2024-12-13 21:20:56

您是否尝试过使用hobbies.length。我还没有测试过这个,但我相信这是在 mongodb 中查询数组范围的正确方法

db.collection.find({$where: '(this.hobbies.length > 0)'})

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

db.collection.find({$where: '(this.hobbies.length > 0)'})
捎一片雪花 2024-12-13 21:20:56

您可以(某种程度上)使用逻辑 $not 通过 $size 运算符检查数组长度范围:

db.collection.find({array: {$not: {$size: 0}}})

You can (sort of) check for a range of array lengths with the $size operator using a logical $not:

db.collection.find({array: {$not: {$size: 0}}})
冰魂雪魄 2024-12-13 21:20:56

这话有些道理。

根据手册

http://www.mongodb.org/display/ DOCS/Advanced+Queries#AdvancedQueries-%24size

$尺寸

$size 运算符匹配任何具有指定数量的数组
元素。以下示例将匹配对象 {a:["foo"]},
因为该数组只有一个元素:

db.things.find( { a : { $size: 1 } } );

您不能使用 $size 来查找大小范围(例如:数组
超过 1 个元素)。如果您需要查询某个范围,请创建一个
添加元素时增加的额外大小字段

因此,您可以检查数组大小是否为 0,但不能检查“大于 0”之类的内容

That's somewhat true.

According to the manual

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size

$size

The $size operator matches any array with the specified number of
elements. The following example would match the object {a:["foo"]},
since that array has just one element:

db.things.find( { a : { $size: 1 } } );

You cannot use $size to find a range of sizes (for example: arrays
with more than 1 element). If you need to query for a range, create an
extra size field that you increment when you add elements

So you can check for array size 0, but not for things like 'larger than 0'

凹づ凸ル 2024-12-13 21:20:56

之前的问题解释了如何处理数组计数问题。尽管在您的情况下,如果 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.

御守 2024-12-13 21:20:56
  1. 如果您只需要查找零个爱好,并且没有为零个爱好的人设置爱好键,请使用 EXISTS 标志。
    添加“爱好”索引以增强性能:

    db.collection.find( { 爱好 : { $exists : true } } );

  2. 但是,如果爱好为零的人有空数组,爱好 1 的人有一个包含 1 个元素的数组,则使用此通用解决方案:

维护一个名为“hcount”(爱好计数)的变量,并在任何更新中始终将其设置为等于爱好数组的大小。
字段“hcount”上的索引
然后,您可以执行如下查询:

 db.collection.find( { hcount : 0 } ) // people with 0 hobbies    
 db.collection.find( { hcount : 5 } ) // people with 5 hobbies

3 - 根据 @JohnPs 的回答,“$size”对于此目的也是一个很好的运算符。
http://www.mongodb.org/display/DOCS/Advanced+查询#AdvancedQueries-%24size

  1. 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 } } );

  2. 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 :

 db.collection.find( { hcount : 0 } ) // people with 0 hobbies    
 db.collection.find( { hcount : 5 } ) // people with 5 hobbies

3 - From @JohnPs answer, "$size" is also a good operator for this purpose.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文