English 中文(简体)
Mongo DB: 如何选择带宽阵列数的项目 页: 1
原标题:Mongo DB: how to select items with nested array count > 0

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

{
    _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" : { &gt : 0}}).limit(10)

并且不产生任何结果。

  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?

最佳回答

您是否使用<代码>。 i n t 测试了这一情况,但我认为,这是正确的方式,可以质疑中贡布阵列的范围。

db.collection.find({$where:  (this.hobbies.length > 0) })
问题回答

在这种具体情况下,你可以使用清单索引解决你的问题:

db. Collection.find({>hobbies.0>:{$exists : real>limit(10)/code>

这确保了第一点内容。 你也可以这样做,通过在范围结束时核对现有要素,确保清单缩短到零之间或x之间和 y之间。

您可以(简短)对<代码>size的一系列阵列长度进行检查。 运营商使用逻辑<代码>$not:

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

先前的问题解释了如何处理阵列计问题。 虽然在您的案例中,如果国际移民组织确实是你想要检验的唯一价值,那么当它空洞并选择不将其序列化时,你就可以将阵列定为无效,但你可以检验这个领域的存在。 当你想给用户增加一只ho子时,应自行测试无效并创建阵列。

就第2号而言,如果你增加了计算点,就很容易从数据库中选择你想要回的领域,并包括计票场。

  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. 收集.find({ho:{ $:真});

  2. 但是,如果拥有零栖息地的人有空阵,而有1个ho子的人有一阵列,有1个元素,则使用这一通用解决办法:

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





相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签