Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2397

How to run shell query directly from c#

      I want to run mongo find query with foreach operation directly on mongo server because through foreach operation I want to insert documents into other collection with extra properties. My mongo query is below

      db.products.find({}).forEach(function(doc){
      //make javascript object dynimacally 
      var newDoc = {
          "user_name": "name",
          "user_action": "insert|update",
          "doc": doc
      }
      db.logCollection.insertOne(newDoc); })
      

      This is successfully run into mongo shell, now I want to execute that script from c# application with modify object value dynamically. I was hoping it would be able to parse those queries and pass them off to database.RunCommand but I have not been able to make any progress.

      var bsonDoc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>("query");
      database.RunCommand(command);
      

      That code fails on the call to Deserialize with this error message: [Additional information: JSON reader was expecting a value but found 'db'.] which makes perfect sense because the script is not valid JSON.

      So I'm having trouble parsing the script into something that can be executed.

       

            Assignee:
            vincent.kam@mongodb.com Vincent Kam (Inactive)
            Reporter:
            AdnanAhmed Adnan Ahmed [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: