1

Let's say I have a string. " db.getCollection("somecollection").find({})". Can I execute this string as a query in C#? i.e. I get a string. And I just execute it as a query but in c#

I just want like this

string query = "db.getCollection("somename")";
Mongo.execute(query);
user425799
  • 19
  • 5

1 Answers1

2

no, the best you can do in this context is to use db.RunCommand<BsonDocument>("{ ping : 1 }") (c#) which is close to the shell db.runCommand({ ping : 1 })

UPDATE: you may look at this as well How to execute mongo commands through shell scripts?, I'm not familiar with this and it doesn't work for me on windows and 5.0 server in most of the cases mentioned there other than simple one: mongo --eval "printjson(db.serverStatus())", but if you will be able to make this suggested script mongo < script.js (or similar) work in, for example, the shell, you will be able to put your random query in this file(script.js) and then, add this file as argument into Process creating similar to:

            using (var process = new Process())
            {
                // arguments below may require the whole path to the files
                process.StartInfo.Arguments = "script.js";
                process.StartInfo.FileName = "mongo"; 

                process.Start();
            }

to read results, you will need analyzing process.StandardOutput/process.StandardError stream.

dododo
  • 3,872
  • 1
  • 14
  • 37
  • But there must be a way to do it. – user425799 Sep 16 '21 at 08:06
  • Mongo doesn't provide any other way other than runCommand, you can reach this goal via reflection or codedom: https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/using-the-codedom, but it will require additional steps – dododo Sep 18 '21 at 21:05
  • however if I understood what you need correctly, runCommand https://docs.mongodb.com/manual/reference/command/ mostlikely is what you need – dododo Sep 18 '21 at 21:07
  • runCommand require JSON format as string. – user425799 Sep 26 '21 at 16:20
  • it requires a MQL command in BSON format. It can be BsonDocument or string. – dododo Sep 26 '21 at 17:24
  • I tried to make a runCommand ("db. getCollection ("dbName")") with interpolation, but it swears that the string is not in the right format. – user425799 Sep 27 '21 at 04:43
  • yes, because it expects MQL command. You still didn't provide a use case why you need this – dododo Sep 27 '21 at 11:18
  • I get a string. And I am sure that there is a mong o request in it. I just have to execute this mongo request. Like this string result = Mongo.execute("db.getCollection('name')"); – user425799 Sep 27 '21 at 17:10
  • there is a mongo shell command, not MQL request, it's different things – dododo Sep 27 '21 at 17:23
  • how can i execute mongo shell command? as above in c#. – user425799 Oct 01 '21 at 07:35
  • you can't do it for exactly above query – dododo Oct 01 '21 at 11:49
  • you still didn't provide details why you need this. `string result = Mongo.execute("db.getCollection('name')")` this string cannot return any result since it will be just a mongo object – dododo Oct 01 '21 at 12:19
  • It's just example. Query maybe like this: Mong.execute("db.getCollection('somedb').find({})"); I have to execute this string as mongo query. Is there any some way do it? – user425799 Oct 04 '21 at 04:43
  • again, it's not possible with exactly code you use. The best you can do is to use `runCommand`. In shell this command will look like: `db.runCommand({find : "collectionName" , filter : {}})`. In c# you will need to use: `db.RunCommand({find : "collectionName" , filter : {}})`. The doc: https://docs.mongodb.com/manual/reference/command/find/ – dododo Oct 04 '21 at 12:36
  • ok, thank you. Just i am getting arbitrary string. That will be mongo query but in string. And i must execute it. I dont know what string will be. I have to execute it. I cannot transform string to bsondocument, dont know what is in string – user425799 Oct 04 '21 at 12:38
  • see my update above – dododo Oct 04 '21 at 16:12
  • hi, i know this way) I worked by this way, but it's not security way) I am finding a way, that will work only in C#. Without working with shell. But thank you for finding answers for me) – user425799 Oct 05 '21 at 05:40