-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

配列の一部のみを取得することはできません。取得は、ドキュメント/埋め込みドキュメントごとにのみ制限できます。

于 2011-07-22T03:19:11.263 に答える
0

ジャバまたはジャバスクリプト?

ジャワ:

//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

ドキュメント スキーマはおそらく改善される可能性がありますが、MongoDB 2.2のAggregation Frameworkは、いくつかの新しい柔軟性を可能にします。

注: あなたの例には、comment_id = 2 の 2 つのコメントがあります。これを使用してコメントを一意に識別する場合、これは間違いのようです。3 番目の 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 に答える