Defining a property within a Bsondocument

Asked

Viewed 180 times

0

Salve galera!

I’m working with c# Mongodb . Net Driver, and there’s something I don’t understand 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 the grouping I have { "_id", "$saleId" } and { "_documentObjId", "$_id" }, the error is in the "_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, including in "_id" If I change to any other name from the same mistake, where do I make that definition? Thanks!

  • Hello, I didn’t quite understand your question. But I believe you the error occurs is because you haven’t mapped the _documentObjId field anywhere. You’ve already made your move?

  • @Jhonathan, in short it’s this, I don’t understand where I can do this mapping, since _id I didn’t do the mapping either, but it works.

  • Have you created a class to handle objects? It would be better to use it instead of Bsondocument.

  • @Jhonathan I researched this before, but I abandoned it because of the performance, according to reports the performance falls a lot like this, and this service I’m creating will generate a data cube and so I need a lot of performance.

  • @Jhonathan , but if there’s no other way I’ll have to create a real class... But my curiosity about _id working without mapping will stay.

  • Good my tips are: Try to test this code on mongoDB, it may be that your aggregation is mounted wrong. Try removing (underline) from the documentObjId variable

  • @Jhonathan, I did as you said, made the same mistake.

Show 3 more comments

1 answer

0


I understand 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 within it I can group by as many Fields as I want and define the names of Fields. Thank you for trying to help @Jhonathan. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.