1

I want to run below native Queries in my java project using DB.runCommand()

Query1:

db.test_temp.insert({entityId:2,columnName1:"columnName2.1",columnName2:"columnName2.2"})

Query 2:

db.test_temp.updateOne(
   { columnName1: "columnName1" },
   {
     $set: { "columnName1": "columnName1.0" }
   }
);

My code:

String json="db.test_temp.insert({entityId:2,columnName1:"columnName2.1",columnName2:"columnName2.2"}";
Bson command = new Document("eval",json); 
db.runCommand(command);

I also tried from How to execute MongoDB native query (JSON) using mongo-java-driver only? but it's not working

Error:

com.mongodb.MongoCommandException: Command failed with error 13 

1 Answer 1

0

As said in your mentioned link `

eval command is deprecated and its usage is not advised

According to this answer, you can do as follows

String json = "{insert : 'collectionName', documents : [{entityId:2,columnName1:'columnName2.1',columnName2:'columnName2.2'}]}";
Document bsonCmd = Document.parse(json);
Document result = db.runCommand​(bsonCmd);

Please check insert command syntax in insert docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.