How to search a document and remove field from it in mongodb using java?
Asked Answered
K

6

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.

Kahaleel answered 1/4, 2015 at 11:37 Comment(4)
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??Guncotton
It may help you .#18456437Afro
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" }Kahaleel
@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.Kahaleel
A
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();
Afro answered 2/4, 2015 at 13:29 Comment(0)
O
3

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

device.updateMany(eq("_id", "whatever"), unset("userId"));
Orvie answered 6/2, 2021 at 10:6 Comment(0)
F
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().

Fictionalize answered 14/2, 2016 at 19:5 Comment(0)
A
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);
Amal answered 2/4, 2015 at 11:0 Comment(0)
S
0

My usecase, if you want drop/unset a column/field in all rows/documents use:

device.updateMany(exists("userId"), unset("userId"));

Synecious answered 12/6 at 10:29 Comment(0)
M
-3

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

Myxomatosis answered 2/4, 2015 at 13:1 Comment(1)
it is not in javaHalt

© 2022 - 2024 — McMap. All rights reserved.