Raj
Raj

Reputation: 415

How to use raw MongoDB aggregation query in C#?

I have the below mongoDB query working perfectly fine in mongoDB shell but wondering how to use that query in C#?

db.collection.aggregate([{
        $match: {
            fieldName: "dsdsd",
            createdAt: {
                $gte: ISODate("2021-07-05T12:29:30.000+00:00"),
    
                $lte: ISODate("2021-07-15T12:29:30.000+00:00")
            }
        }
    }, {
        $group: {
            _id: {
                $dateToString: {
                    format: "%Y-%m-%d-%H",
                    date: "$createdAt"
                }
            },
            items: {
                $first: '$$ROOT'
            }
        }
    },{"$replaceRoot":{"newRoot":"$items"}}
    ,{"$sort":{"createdAt":-1}}
    
    ])

I want to use the below raw query in c# something like below:

var pipeline = {
            $match: {
                fieldName: "dsdsd",
                createdAt: {
                    $gte: ISODate("2021-07-05T12:29:30.000+00:00"),
        
                    $lte: ISODate("2021-07-15T12:29:30.000+00:00")
                }
            }
        }, {
            $group: {
                _id: {
                    $dateToString: {
                        format: "%Y-%m-%d-%H",
                        date: "$createdAt"
                    }
                },
                items: {
                    $first: '$$ROOT'
                }
            }
        },{"$replaceRoot":{"newRoot":"$items"}}
        ,{"$sort":{"createdAt":-1}}
        

var result = await _mongoDbContext.model.Aggregate(pipeline).ToListAsync();

Upvotes: 1

Views: 2445

Answers (1)

dododo
dododo

Reputation: 4857

you can add any custom stage via AppenStage

collection
    .Aggregate()
    .AppendStage<BsonDocument>(BsonDocument.Parse("stage1"))
    .AppendStage<BsonDocument>(BsonDocument.Parse("stage2"))
    ..

or

        var pipeline = new EmptyPipelineDefinition<BsonDocument>()
            .AppendStage<BsonDocument, BsonDocument, BsonDocument>(BsonDocument.Parse("stage1"))
            .AppendStage<BsonDocument, BsonDocument, BsonDocument>(BsonDocument.Parse("stage2"));

        collection.Aggregate(pipeline).ToList();

UPDATE: you can also use a shell-like syntax for db.runCommand (which is harder):

MongoDB Enterprise mongos> db.runCommand({ aggregate: 'test', pipeline: [ {stage1_json}, {stage2_json} ], cursor: {}  })
...

where the c# equivalent is:

var result = db.RunCommand<BsonDocument>("{ aggregate : 'test', pipeline:  [ {stage1_json}, {stage2_json} ], cursor: {} }"); 

Upvotes: 2

Related Questions