Ramakrishna
Ramakrishna

Reputation: 5038

Aggregate Query Conversion From Mongo DB to C#

I want to Convert below Mongo Query(Written in MongoVUE UI Editor) to 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)
            };

it is compiling successfully but when running it is returning Error like

.NET type MongoDB.Bson.BsonElement cannot be mapped to a BsonValue

I debugged and find out error lies in below block of query

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

But I am not able to find out what error is.

Mongo C# Driver is 2.0.X Version

Can any one help me to convert that query to be a working query

EDIT Structure of my data is(to test:create a temp collection and insert below)

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

Upvotes: 2

Views: 3870

Answers (1)

Paparaju Gowd
Paparaju Gowd

Reputation: 34

Please try with below Query :

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

Upvotes: 1

Related Questions