从 Mongo DB 到 C# 的聚合查询转换

Aggregate Query Conversion From Mongo DB to C#

我想将下面的 Mongo 查询(用 MongoVUE UI 编辑器编写)转换为 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 type MongoDB.Bson.BsonElement cannot be mapped to a BsonValue

调试发现错误在下面的查询块

new BsonDocument("$match", new BsonDocument("$and",
                    new BsonArray(new BsonDocument("Users.UserId", new BsonDocument("$all", 
                        new BsonArray().Add(paginationData.UserId).Add(paginationData.Id))))))

但是我无法找出是什么错误。

Mongo C# Driver is 2.0.X Version

任何人都可以帮助我将该查询转换为有效查询

编辑 我的数据结构是(到 test:create 一个临时集合并在下面插入)

{  "_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
}]}

请尝试以下查询:

var aggregateArgs = new AggregateArgs();
                aggregateArgs.Pipeline = new[]    
                {
                    new BsonDocument("$match", new BsonDocument("$and",
                        new BsonArray().Add(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)
                };