我有这样的集合:
{
"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等
答案 0 :(得分:0)
无法仅检索数组的某些部分。您可以限制仅按文档/嵌入文档提取。
答案 1 :(得分: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);
......
答案 2 :(得分:0)
虽然您的文档架构可能会得到改进。但MongoDB 2.2中的Aggregation Framework可以带来一些新的灵活性。
注意:在您的示例中,您对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
}