从mongodb到c#的聚合查询转换

本文关键字:查询 转换 mongodb | 更新日期: 2023-09-27 18:12:14

我想将下面的Mongo查询(在mongoue 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"}} }

I tried like

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

但是我不知道什么是错误。

Mongo c# Driver是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
}]}

从mongodb到c#的聚合查询转换

请尝试以下查询:

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)
                };