4

I want to execute CRUD operations with java likeupdateOne(),updateMany() or deleteMany() etc. But when I want to run with operators like $set, $unset I have to import new classes like Updates or create nested Document objects. I want to insert JSON query as native Mongodb uses. Ex: myCollection.updateOne(Json_String_filter,Query_with_operoters_like_$set_as_Json_string);

1 Answer 1

2

Use Document.parse(String json) from org.bson.Document. It returns Document object. Here is an example from Official MongoDb tutorial.

Original:

{
     $set: { "size.uom": "cm", status: "P" },
     $currentDate: { lastModified: true }
   }

You can run in java as:

collection.updateMany(new Document(),Document.parse("{\n" +
                "     $set: { \"size.uom\": \"cm\", status: \"P\" },\n" +
                "     $currentDate: { lastModified: true }\n" +
                "   }"));
Sign up to request clarification or add additional context in comments.

1 Comment

That was the way I looking for.

Your Answer

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