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);
Add a comment
|
1 Answer
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" +
" }"));