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 
James Z
  • 12,209
  • 10
  • 24
  • 44
StudentL
  • 55
  • 1
  • 5

1 Answers1

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