Aggregate in C# returns nothing?

Asked

Viewed 55 times

1

I made a direct query on the console that returned me what I expected, the query in Mongo is like this.

db.AtividadeResultMongo.aggregate([{$unwind: "$LsVariaveisProcesso"},
    {$match: {
        IdAtividade : 913,
        IdEstadoInstanciaAtividade : 6,
        LsVariaveisProcesso : { $regex : /^idato/i }
    }},
    {$project: {
        LsVariaveisProcesso: 1
    }},
    {$group: { _id: "$LsVariaveisProcesso" }},
    {$count: "total"}])

when I execute return me total : 113 which is correct, has values at the base, but in the C# nothing returns and I need help to set it up in c#, someone can help me?

I tried to start like this

var agg = ObterCollection("AtividadeResultMongo")
          .Aggregate()
          .Match(new BsonDocument { 
            { "IdAtividade", "913" }, 
            { "IdEstadoInstanciaAtividade", "6" } });

var teste = await agg.ToListAsync();

but in the variable test has nothing, the connection is correct with the base is correct, I ran a Count and returned value

ObterCollection("AtividadeResultMongo")
       .AsQueryable()
       .Count()
  • Thiago the collection AtividadeResultMongo you need to do what, like forget the console part ?

  • So inside my Lsvariablesuccess I have a string array, in it can have a string like "idAto:9999" that comes from another system, I only record, so the user asked me to group by a field that he will inform in his system, in case it will want to total documents that have within this array "idAto" however there may be more than one document with the same idAto, then looking at the query there, if I take the group of Lsvariaveisprocess is returning 580 items, because there are documents with the same idAto

  • with Where and contains se can filter this Thiago, already tried ... what is more difficult is that there is no way to reproduce, but, I do it in mongodb easily with C#

  • Type: ObterCollection("AtividadeResultMongo").where(x => x.IdAtividade == "913" && x.IdEstadoInstanciaAtividade == "6" && LsVariaveisProcesso.Contains("idato")).Sum() have tried?

  • I just got home and tried to run what you went through, but I couldn’t find it. Where in Imongocollection, did you miss any using? I put Asqueryable<T> before but didn’t roll Sum pq just right with Asqueryable<int> Even so I understood your idea, but where are you grouping by idAto? I tried to make a group with contains the Mongodb.Driver.Linq said that "Contains of type System.String is not supported in the Expression Tree {x}. Contains("idAto")"

  • I’ve tried this in the afternoon, I’ve been at it all day, and it didn’t, I always made that same mistake.

  • Complicated to say

Show 2 more comments
No answers

Browser other questions tagged

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