MongoDB :How to query the item in a list of itmes that is list type?
Test Code:
开发者_JAVA技巧[
{
"_id": {
"$oid": "4e27f4c0cfdb4a09b8ace1dd"
},
"Description": "NO.000001",
"Title": "PM:000001",
"Age": 14,
"CommentList": [
{
"_id": {
"$oid": "4e27f4c0cfdb4a09b8ace1da"
},
"Content": "Hello:00001",
"Creator": "Jack00001",
"Date": "2011-7-21 0:00:00",
"InDate": {
"$date": 1310400000000
}
},
{
"_id": {
"$oid": "4e27f4c0cfdb4a09b8ace1db"
},
"Content": "Hello:00002",
"Creator": "Jack00002",
"Date": "2011-7-21 0:00:00",
"InDate": {
"$date": 1310400000000
}
},
{
"_id": {
"$oid": "4e27f4c0cfdb4a09b8ace1dc"
},
"Content": "Hello:00003",
"Creator": "Jack00003",
"Date": "2011-7-21 0:00:00",
"InDate": {
"$date": 1310400000000
}
}
]
}
]
I use the mongodb c# driver,and based on the data model, my expected results is that:
[
{
"_id": {
"$oid": "4e27f4c0cfdb4a09b8ace1dd"
},
"Description": "NO.000001",
"Title": "PM:000001",
"Age": 14,
"CommentList": [
{
"_id": {
"$oid": "4e27f4c0cfdb4a09b8ace1db"
},
"Content": "Hello:00002",
"Creator": "Jack00002",
"Date": "2011-7-21 0:00:00",
"InDate": {
"$date": 1310400000000
}
},
...
]
}
]
How to get the comment?Thanks!
As I know, it is not possible for now to select a embedded document. Using LINQ on cursor you can filter the result:
var query = Query.And(new[] { Query.EQ("_id.$oid", documentId), Query.EQ("CommentList._id.$oid", commentId) });
var res = collection.FindOne(query);
res = res.Select(o=> new DocumentName
{
Description = o.Description,
....
CommentList = o.CommentList.Where(x=>x.Id == commentId)
});
精彩评论