简体   繁体   中英

How to implement CommandDocument in MongoDB C# Driver version 2.1 without using the Legacy Driver

I am upgrading an app that was using the Mongo v1 C# driver on the 3.x series engine. I have upgraded the DB to 4.2 and the C# driver to 2.1 and made all of the necessary changes related to depreciated c# methods, etc. However, there is one issue remaining. I have a couple of places where we run something similar to:

    var command = new CommandDocument { { "dbStats", 1 }, { "scale", 1 } };
                        var result = mongoDb.RunCommand<BsonDocument>(command);
                        var tempSize = result["dataSize"].ToInt32();

I DO NOT want to load or use the "legacy driver".

What is the proper way using the new (non-legacy) c# driver to execute this type of command? Thanks in advance for everyone's suggestions.

There's now a base abstract class of Command<TResult> where there is derived type of BsonDocumentCommand<BsonDocument> this is similar to what you were using before.

The following code will do the same as your previous code

var client = new MongoClient();
var database = client.GetDatabase("A");

var result = database.RunCommand(new BsonDocumentCommand<BsonDocument>(new BsonDocument {{"dbStats", 1}, {"scale", 1}}));

var tempSize = result["dataSize"].ToInt32();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM