-1

我有这样的收藏:

{
  "Post": {
    "post": "These are following example of your station of the various stamps, and this can't be foured by the name.\n\n          also I can't use this way to search string in the midle way, also what are you doing is the default factory",
    "like": [
      "rudi",
      "tabootie",
      "oknoorap",
      "various",
      "rusian_roulette"
    ],
    "Comment": [
      {
        "comment_id": 1,
        "name": "Anonymous",
        "comment": "You are absolutely right dude, when you call me, you can host here",
        "like": [
          "rudi",
          "stumble",
          "upon",
          "facebook"
        ]
        "timestamp": {
          "t": 9000,
          "i": 1311245225
        }
      },
      {
        "comment_id": 2,
        "name": "Anonymous",
        "comment": "the guy is here",
        "like": [
          "rudi",
          "stumble",
          "upon",
          "facebook"
        ]
        "timestamp": {
          "t": 10000,
          "i": 1311245225
        }
      },
      {
        "comment_id": 2,
        "name": "Oknoorap",
        "comment": "the other guy is here",
        "like": [
          "rudi",
          "stumble",
          "upon",
          "facebook"
        ]
        "timestamp": {
          "t": 11000,
          "i": 1311245225
        }
      }
    ]
  }
}

你可以帮帮我吗?如何仅检索 Post.Comment.comment_id = 2、否定 _id、post 等

4

3 回答 3

0

尽管您的文档模式可能会得到改进.. MongoDB 2.2 中的聚合框架提供了一些新的灵活性。

注意:您的示例中有两条 comment_id = 2 的评论,如果您使用它来唯一标识评论,这似乎是一个错误。我假设您的第三个comment_id 实际上应该是comment_id = 3。

这是一个使用注释的示例aggregate()

db.posts.aggregate(

    // Find specific post
    { $match : {
        '_id' : 123,
    }},

    // Unwind the Post.Comment array into a stream of documents
    { $unwind : '$Post.Comment' },

    // Match specific comment_id
    { $match : {
        'Post.Comment.comment_id' : 2,
    }},

    // Limit results to the embedded Comment
    { $project: {
        'Post.Comment': 1
    }}
)

..和结果:

{
    "result" : [
        {
            "_id" : 123,
            "Post" : {
                "Comment" : {
                    "comment_id" : 2,
                    "name" : "Anonymous",
                    "comment" : "the guy is here",
                    "like" : [
                        "rudi",
                        "stumble",
                        "upon",
                        "facebook"
                    ],
                    "timestamp" : {
                        "t" : 10000,
                        "i" : 1311245225
                    }
                }
            }
        }
    ],
    "ok" : 1
}
于 2012-09-11T14:30:51.390 回答
0

Java还是Javascript?

爪哇:

//Query items
BasicDBObject query = new BasicDBObject();
query.put("Post.Comment.comment_id", 2);

//Show items
BasicDBObject showField = new BasicDBObject();
showField.put("post", 1); //show
showField.put("other", 0); //hide

//Execute query
DBCursor cursor = dbc.find(query, showField);

......
于 2012-08-01T03:12:09.737 回答
0

不可能只检索数组的一部分。您可以限制仅获取每个文档/嵌入文档。

于 2011-07-22T03:19:11.263 回答