title: Aggregate Query Conversion From Mongo DB to C#
body: <p>I want to Convert below Mongo Query(Written in MongoVUE UI Editor) to C#</p>
  
  <pre><code>{"$match": {"$and": [{"Users.UserId": {"$all": ["55dbf74d6ada572168fa98b7","55dbf74d6ada572168fa98b8"]}}]}},{ "$unwind": "$Posts"},{ "$sort": {"Posts.CreatedOn": -1}},{ "$skip": 0 },{ "$limit": 11 },{ "$group": {"_id": "$_id", "Posts": {"$push": "$Posts"}} }
  </code></pre>
  
  <p>I tried like</p>
  
  <pre><code>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)
              };
  </code></pre>
  
  <p>it is compiling successfully but when running it is returning Error like</p>
  
  <blockquote>
    <p>.NET type MongoDB.Bson.BsonElement cannot be mapped to a BsonValue</p>
  </blockquote>
  
  <p>I debugged and find out error lies in below block of query</p>
  
  <pre><code>new BsonDocument("$match", new BsonDocument("$and",
                      new BsonArray(new BsonDocument("Users.UserId", new BsonDocument("$all", 
                          new BsonArray().Add(paginationData.UserId).Add(paginationData.Id))))))
  </code></pre>
  
  <p>But I am not able to find out what error is.</p>
  
  <blockquote>
    <p>Mongo C# Driver is 2.0.X Version</p>
  </blockquote>
  
  <p>Can any one help me to convert that query to be a working query</p>
  
  <p><strong>EDIT</strong>
  Structure of my data is(to test:create a temp collection and insert below)</p>
  
  <pre><code>{  "_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
  }]}
  </code></pre>