[CSHARP-2397] How to run shell query directly from c# Created: 26/Sep/18  Updated: 27/Oct/23  Resolved: 14/Nov/18

Status: Closed
Project: C# Driver
Component/s: Command Operations, Feature Request
Affects Version/s: 2.7.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Adnan Ahmed [X] Assignee: Vincent Kam (Inactive)
Resolution: Works as Designed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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.

 



 Comments   
Comment by Adnan Ahmed [X] [ 16/Nov/18 ]

Thanks, Vincent for the suggestion. Please inform me if it is possible, in future mongo team oppose better and optimize way to insert multiple docs with changing value into other collection against a single request from the application like in SQL Server, we have an option of insert records into another table after change values and perform all action through stored procedure against a single request.

 

Thanks

Comment by Vincent Kam (Inactive) [ 15/Nov/18 ]

HI Adnan,

Ah, I see, my apologies for the confusion. Thank you for your feature request! We currently are not planning on adding the ability parse and execute Javascript via the .NET driver's RunCommand method.

That being said, if you truly wish to mix C# and Javascript, you may also be able to use coll.MapReduce to handle your particular example (please see https://docs.mongodb.com/manual/core/map-reduce/).

Kind regards,
Vincent

Comment by Adnan Ahmed [X] [ 15/Nov/18 ]

Hi Vincent,

Thanks for checking my request, Actually I posted that question for the proposed feature on mongo .NET library because I think that in future, anyone wants to use javascript shell command through .NET library. Which will be more optimized and fastest way to retrieve their desired complex results. 

Comment by Vincent Kam (Inactive) [ 14/Nov/18 ]

Hello Adnan,

Thank you for submitting your question! Unfortunately, this Jira project is for reporting bugs or requesting features for the MongoDB C#/.NET driver. Another forum may be more appropriate for your question, such as as StackOverflow or the mongodb-user Google group.

That being said, it appears that you are trying to run a combination of Javascript/mongo shell commands (e.g. using Javacript's forEach method and mongo shell's db variable) through the .NET driver's RunCommand method, which is something the .NET driver currently does not support at the moment.

Kind regards,
Vincent

Generated at Wed Feb 07 21:42:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.