English 中文(简体)
MongoDB - Query by sub-tree
原标题:MongoDB - Query by sub-tree
  • 时间:2011-10-26 06:07:17
  •  标签:
  • mongodb

我的文件结构与以下简化版本相同:

{
  some_other_props: { ... },
  systems: {
    sys1: {
      score: 24,
      metric: 52
    },
    another_sys: {
      score: 9,
      metric: 77
    },
    some_other_sys: {
      score: 12,
      metric: 5
    }
  }
}

我愿交回<代码>核心>的所有文件:{“美元”:15 }对于任何次文件都是如此。

我认为,这样做的唯一途径是将钥匙清单列入<编码> 系统,并将这些钥匙编成某种或多份说明。 但是,似乎像我这样作错了。

我可以改革文件结构,以便每个系统都列入自己的文件......但是,在<代码>>和其他——其他_props<>/code”中还有许多其他信息,而现在的重新定位将是痛苦的。

事实上,我试图做一些比较复杂的事。 退还同一系统<代码>核心和<代码>>>>>>>之间差别很大的所有文件。 但是,在我能做事之前,我希望确保我能够像我前面所显示的那样,在小树上做简单的询问。

是否有任何人建议如何在莫戈亚提子?

问题回答

You should consider storing the sub-documents as an array:

{
  some_other_props: { ... },
  systems: 
    [ {id:  sys1 ,
      score: 24,
      metric: 52
    },
    { id:  another_sys ,
      score: 9,
      metric: 77
    }]

}

然后,你只能问询<代码>限量系统:{>格特:15 },而且你还可以制定系统索引。 如果你认为需要的话,就得分。

If you now exactly what is getting scored you can make a fat "or" query :

$or : [{ systems.sys1.score : { "$gte" : 15 } }, {systems.another_sys.score : { "$gte" : 15 }}]

but it seems like a lame way to solve the problem. so my suggestion is make a different catalog for system_scores which have a many to one reference to system_props collection. That would make it much more easier to query.





相关问题
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</...

热门标签