Define a property within a BsonDocument

0

Save Galley!

I'm working with C # MongoDB .Net Driver, and have something I'm not understanding in the queries, follow the example:

    var dataBase = mongoClient.GetDatabase(dbName);
    var transactionsColl = dataBase.GetCollection<BsonDocument>(transactionCollectionName);

    var mongoSales = transactionsColl.Aggregate()
                            .Match(BsonDocument.Parse("{ 'saleDate': { $lte: ISODate('" + timeStamp.ToString("o") + "') } }"))
                            .Group(new BsonDocument { { "_id", "$saleId" }, { "_documentObjId", "$_id" } }).ToListAsync().Result;

In grouping I have { "_id", "$saleId" } and { "_documentObjId", "$_id" } , error is in "_documentObjId" , causes the following exception:

{"Command aggregate failed: the group aggregate field '_documentObjId' must be defined as an expression inside an object."}

I had understood that I could put any name as property of the document, but the error is not so simple, even in "_id" if I change to any other name the same error, where do I make this definition? Thanks!

    
asked by anonymous 02.03.2016 / 18:25

1 answer

0

I understood the syntax:

var mongoSales = transactionsColl.Aggregate()
                        .Match(BsonDocument.Parse("{ 'saleDate': { $lte: ISODate('" + timeStamp.ToString("o") + "') } }"))
                        .Group(new BsonDocument { {"_id", new BsonDocument { { "document", "$document" }, { "saleDate", "$manticSaleDate" } } } }).ToListAsync().Result;

I have to create a new BsonDocument (_id) and inside it I can group by as many fields as I want and in it I define the names of the fields. Thanks for trying to help @Jhonathan. :)

    
04.03.2016 / 18:01