English 中文(简体)
Mongodb: 文件中的系列内容
原标题:Mongodb: Querying array elements within a document
  • 时间:2011-10-07 21:05:55
  •  标签:
  • mongodb

我要问一下:

  • return all documents
  • up to 2 comments (e.g. a slice, either 0, 1, or 2 comments)
  • all comments must have views > 10

似乎我需要建立一种职能,单独评价每份文件,但是,这并不明确,特别是鉴于我想要做一些切题,并回到符合这些标准的零件。

样本:

{
    title: "One",
    comments: [
        {
            title: "comment1",
            views: 9
        },
        {
            title: "comment2",
            views: 10
        },
        {
            title: "comment3",
            views: 11
        },
        {
            title: "comment4",
            views: 12
        },
    ]
}

我想做的是:

db.collection.find({"comments.views": {$gt: 10}}, {comments:{$slice: 2}})

但是,这两份文件都附有评论;10项意见,然后提出2项意见。 我想回头谈谈有十项内容的评论。 我不能在客户上这样做,也不能在不失去一些评论的情况下使用美元,因此,我需要就行文做。 想法?

最佳回答

But this returns any document with a comment with > 10 views, and then slices 2 comments

这是过滤多层嵌入文件的行为,通常配对过滤器将退回整个文件,而不是分页。

通常使用固定运营商的美元与更新的次级文件相匹配。 但是,这一特征尚未在回报投机者身上得到实施。

mongo支持田地运营商返回Specifier。 (如果你真的需要,请原投票)

因此,你必须处理

  • up to 2 comments (e.g. a slice, either 0, 1, or 2 comments)

部分通过所有收藏品申请。

问题回答

你们可以看一看整体。 您先得减去,然后使用过滤器:

db.<your collection>.aggregate( [ { $unwind : "$comments" }, 
    {$match:{"comments.views": {$gt: 10}}} ] )

检查结果,并在此基础上实施其他行动





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

热门标签