0
我想下面蒙戈查询(用MongoVUE UI编辑器)转换为C#汇总查询转换,从蒙戈DB到C#
{"$match": {"$and": [{"Users.UserId": {"$all": ["55dbf74d6ada572168fa98b7","55dbf74d6ada572168fa98b8"]}}]}},{ "$unwind": "$Posts"},{ "$sort": {"Posts.CreatedOn": -1}},{ "$skip": 0 },{ "$limit": 11 },{ "$group": {"_id": "$_id", "Posts": {"$push": "$Posts"}} }
我试着像
var aggregateArgs = new AggregateArgs();
aggregateArgs.Pipeline = new[]
{
new BsonDocument("$match", new BsonDocument("$and",
new BsonArray(new BsonDocument("Users.UserId", new BsonDocument("$all",
new BsonArray().Add(paginationData.UserId).Add(paginationData.Id)))))),
new BsonDocument("$unwind", "$Posts"),
new BsonDocument("$group", new BsonDocument
{
{"_id", "$_id"},
{"Posts", new BsonDocument("$push", "$Posts")},
}),
new BsonDocument("$sort", new BsonDocument("Posts.CreatedOn", -1)),
new BsonDocument("$skip", paginationData.StartIndex - 1),
new BsonDocument("$limit", paginationData.PageSize)
};
它成功编译,但是当运行它正在返回错误如
.NET类型MongoDB.Bson.BsonElement无法映射到BsonValue
我调试,并找出错误在于以下查询
new BsonDocument("$match", new BsonDocument("$and",
new BsonArray(new BsonDocument("Users.UserId", new BsonDocument("$all",
new BsonArray().Add(paginationData.UserId).Add(paginationData.Id))))))
块,但我无法找出是什么错误。
蒙戈C#驱动程序是2.0.X版本
任何一个可以帮助我到查询转换为工作查询
编辑我的数据 结构(测试:创建一个临时集合并在下面插入)
{ "_id" : ObjectId("55f15ead6ada543f386d9f5e"), "Users" : [{ "UserId": "55ed59186ada5750b0eb0f1f" }, { "UserId" : 55dbf74d6ada572168fa98b7" }], "Posts" : [{
"Name" : "Navy Blue Solid Polo T-Shirt",
"Description" : "Look your casual best wearing this blue coloured polo T-shirt from Lee. Designed for modern men, this polo T-shirt will allow you to step out in style. Offering great comfort all day long, this regular-fit T-shirt is made from cotton that makes it soft against the skin and comfortable to wear all day long. It can be best teamed with a pair of jeans and moccasins.",
"Location" : {
"type" : "Point",
"coordinates" : [78.44651, 17.437426]
},
"Address" : "Srinivasa Nagar",
"LocationId" : "55dc77b46ada561bfcc5bf2e",
"Images" : [{
"URL" : "http://xxx.blob.core.windows.net/postimages/157c11a8-37a8-4b52-983a-2ddcb03d35ca.jpg"
}, {
"URL" : "http://xxx.blob.core.windows.net/postimages/20b2d5bc-2024-41c1-a43c-c571c1b82d11.jpg"
}],
"CreatedById" : "55dbf74d6ada572168fa98b7",
"CreatedOn" : ISODate("2015-09-10T12:40:57.125Z"),
"IsActive" : true
}, {
"Name" : "Navy Blue Solid Polo T-Shirt",
"Description" : "Look your casual best wearing this blue coloured polo T-shirt from Lee. Designed for modern men, this polo T-shirt will allow you to step out in style. Offering great comfort all day long, this regular-fit T-shirt is made from cotton that makes it soft against the skin and comfortable to wear all day long. It can be best teamed with a pair of jeans and moccasins.",
"Location" : {
"type" : "Point",
"coordinates" : [78.44651, 17.437426]
},
"Address" : "Srinivasa Nagar",
"LocationId" : "55dc77b46ada561bfcc5bf2e",
"Images" : [{
"URL" : "http://xxx.blob.core.windows.net/postimages/157c11a8-37a8-4b52-983a-2ddcb03d35ca.jpg"
}, {
"URL" : "http://xxx.blob.core.windows.net/postimages/20b2d5bc-2024-41c1-a43c-c571c1b82d11.jpg"
}],
"CreatedById" : "55dbf74d6ada572168fa98b7",
"CreatedOn" : ISODate("2015-09-10T12:45:16.559Z"),
"IsActive" : true
}]}