Calculate field in a projection mongodb c#
NickName:M. Saponara Ask DateTime:2017-03-14T01:38:02

Calculate field in a projection mongodb c#

I have the following query in mongoDB

pdb.getCollection('articulo').aggregate(
   {
        $match: { $text: { $search: "monitor" } } ,
   },
   {
        $project: {
             _id: 1,
            titulo :1,               
            ranking : {
                $avg: "$valoracion.valor"
            }
        }
    },
    {
       $sort : { 
                ranking : -1 , fechaAlta : -1               
           }         
    });

And I'm trying to convert it to a C # query (MongoDB.Driver), but I have a problem with the calculated field (average) in the projection.

My question is: How can I get a calculated field ("ranking") in the projection?

This is what I have:

 public async Task<List<T>> All<T>(string searchBy, int page, int size) where T : class, new()
    {
        try
        {
            var projectionSelect = Builders<T>.Projection.Include("_id")
                                                                    .Include("titulo")
                                                                    //.Include(new BsonDocument { { "rating", new BsonDocument { { "$avg", "$valoracion.valor" } } } }); that didn´t wokt for me


            return await _dataBase.GetCollection<T>(GetName<T>()).Aggregate().Project<T>(projectionSelect)                    
             .Sort(Builders<T>.Sort.Descending("ranking")).Skip(page).Limit(size).ToListAsync();


        }
        catch (Exception ex)
        {
            Log.Instance.LogErrors(ex);
            throw;
        }
    }

I appreciate the help in advance

Copyright Notice:Content Author:「M. Saponara」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42769971/calculate-field-in-a-projection-mongodb-c-sharp

More about “Calculate field in a projection mongodb c#” related questions

Calculate field in a projection mongodb c#

I have the following query in mongoDB pdb.getCollection('articulo').aggregate( { $match: { $text: { $search: "monitor" } } , }, { $project: { _id: 1, ..

Show Detail

How to control the projection definition in MongoDb using C#

I have a domain class like this. public class Thing { public Guid Id { get; set; } public string Name { get; set; } public Dictionary&lt;string, string&gt; Stuff { get; set; } } I'm

Show Detail

MongoDB projection in elixir returning error

I used this https://github.com/ankhers/mongodb driver to run queries on my Elixir application. The doc specifies that the Mongo.find(...) function returns specified fields with projection option....

Show Detail

mongodb java driver hide id field in aggregation/projection operation

I'm performing an aggregation operation using the java mongodb driver, and I followed the example from the docs (pasted below). According to this, the _id field should be hidden. However, in my

Show Detail

MongoDB - PHP - Unsupported projection option

The following command works from the mongo command line: db.users.aggregate([ {'$match': 'eventDate': {$gte: ISODate("2015-01-01T00:00:00.0Z"), $lte: ISODate("2017-01-01T00:...

Show Detail

Spring Data MongoDB - projection and search

I am using "Wildcard text index" in order to search for a pattern in every fields of my class. I am also using projection in order to remove a certain field: @Query(value = "{$text: { $search: ?0 },

Show Detail

Examine query generated from MongoDB projection by C# driver

I'm querying data with C# driver for mongodb using projection. The code is like: var filter = Builders&lt;User&gt;.Filter.Eq("_id", userId); var projection = Builders&lt;User&gt;.Projection.Expres...

Show Detail

Rename field in projection when using Java driver

I would like to rename a field in a projection but this does not work when using Kotlin and the MongoDB Java driver. Can this be done with the Java driver? collection .find() //...

Show Detail

MongoDB: projection on specific fields of all objects in an array

I have a MongoDB database with following data structure for one Part: Part Data structure Now I want to to have a projection on the element Descriptionof every object in the array SensorData. Using...

Show Detail

MongoDB Indexing and Projection

I have a few questions about MongoDB: (1) Does indexing help with projection? (2) I have assigned a collection a number of indexes and tried to run a find with sort, and then use explain, it shows

Show Detail