How to search a document and remove field from it in mongodb using java? -
i have device
collection.
{ "_id" : "10-100-5675234", "_type" : "device", "alias" : "new alias name", "claimcode" : "fg755df8n", "hardwareid" : "serail02", "isclaimed" : "true", "model" : "vmb3010", "userid" : "5514f428c7b93d48007ac6fd" }
i want search document _id
, update after removing field userid
result document. trying different ways none of them working. please me.
you can remove field using $unset
mongo-java driver in way:
mongoclient mongo = new mongoclient("localhost", 27017); db db = (db) mongo.getdb("testdb"); dbcollection collection = db.getcollection("collection"); dbobject query = new basicdbobject("_id", "10-100-5675234"); dbobject update = new basicdbobject(); update.put("$unset", new basicdbobject("userid","")); writeresult result = collection.update(query, update); mongo.close();
find out more @ : http://www.journaldev.com/4334/mongodb-update-document-set-all-example-mongo-shell-java-driver
Comments
Post a Comment