8

I have a device collection.

{
   "_id" : "10-100-5675234",
   "_type" : "Device",
   "alias" : "new Alias name", 
   "claimCode" : "FG755DF8N", 
   "hardwareId" : "SERAIL02",
   "isClaimed" : "true",
   "model" : "VMB3010", 
   "userId" : "5514f428c7b93d48007ac6fd" 
 }

I want to search document by _id and then update it after removing a field userId from the result document. I am trying different ways but none of them is working. Please help me.

Dev
  • 13,492
  • 19
  • 81
  • 174
Swapnil1988
  • 269
  • 3
  • 6
  • 18
  • Can you please provide brief information about what you did so far and provide more description like sample document, field that you want to remove from that document etc?? – Vishwas Apr 01 '15 at 13:27
  • It may help you .http://stackoverflow.com/questions/18455937/remove-id-from-mongodb-result-java – Dev Apr 01 '15 at 13:37
  • my collection is like this and i want to search collection it through _id and then remove the userid field from database. { "_id" : "10-100-5675234", "_type" : "Device", "alias" : "new Alias name", "claimCode" : "FG755DF8N", "hardwareId" : "SERAIL02", "isClaimed" : "true", "model" : "VMB3010", "userId" : "5514f428c7b93d48007ac6fd" } – Swapnil1988 Apr 02 '15 at 09:00
  • 1
    @dev What this link provide is removal of a field from result of some get operation. I want something like update where we provide search and remove. – Swapnil1988 Apr 02 '15 at 09:06

5 Answers5

11

You can remove a field using $unset with mongo-java driver in this 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();
Dev
  • 13,492
  • 19
  • 81
  • 174
2

The easiest way is to use the functionality in the java driver:

Query query = new Query();
query.addCriteria(Criteria.where("_id").is(new ObjectId("10-100-5675234")));
Update update = new Update();
update.unset("userId"); //the fields you want to remove
update.set("putInYourFieldHere", "putInYourValueHere"); //the fields you want to add
mongoTemplate.updateFirst(query, update, Device.class);

The above code assumes that your "_id" is your mongodb normal "_id" which means that the variable you are looking for must be encased in the new ObjectId().

Simon
  • 19,658
  • 27
  • 149
  • 217
2

Long time since this post was opened, but might be useful for someone in the future.

device.updateMany(eq("_id", "whatever"), unset("userId"));
Calfa
  • 233
  • 4
  • 11
0

An ugly way is to replace the old version with the new version of you document (no userid).

BasicDBObject newDocument = new BasicDBObject();
newDocument.put("_type", "Device");
newDocument.put("alias", "new Alias name");
// ...  

BasicDBObject searchQuery = new BasicDBObject().append("_id", "10-100-5675234");

collection.update(searchQuery, newDocument);
annkitkat
  • 11
  • 3
-3

The MongoDB documentation provides a clear answer to this question: use the $unset update operator.

jyemin
  • 3,743
  • 23
  • 16