简体   繁体   中英

MongoDb's $set equivalent in its java Driver

Is there a way in which I can modify the value of one of the keys in MongoDb via its Java Driver. I tried out the following:

someCollection.update(DBObject query, DBObject update);
someCollection.findAndModify(DBObject query, DBObject update);

But both the functions completely replace the queried document with the updated document. What is the way to update only one of the value of a particular key as in the case of using $set in the mongo shell.(apart from making a completely new Document with all fields copied and one of the fields updated).

BasicDBObject carrier = new BasicDBObject();
BasicDBObject query = new BasicDBObject();
query.put("YOUR_QUERY_STRING", YOUR_QUERY_VALUE);

BasicDBObject set = new BasicDBObject("$set", carrier);
carrier.put("a", 6);
carrier.put("b", "wx1");        
myColl.updateMany(query, set);

This should work, the answer which is accepted is not right above.

Try something like this:

BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("age", 10));
set.append("$set", new BasicDBObject("name", "Some Name"));
someCollection.update(someSearchQuery, set);

Also look at this example .

None of the solutions mentioned above worked for me. I realized that the query should be a Document type and not a BasicDBObject :

Document set = new Document("$set", new Document("firstName","newValue"));

yourMongoCollection.updateOne(new Document("_id",objectId), set);

Where "yourMongoCollection" is of type "MongoCollection" and "objectId" of type "ObjectId"

First, unless I want to reconfigure/reformat/"re-type" my values I'd go only with findAndModify and not update .

Here is a fully working example for c&p purposes... Enjoy:

    Boolean updateValue(DB db, DBCollection collection, String id, String key, Object newValue)
    {
        DBCollection collection = db.getCollection(<collection name>);

        // Identify your required document (id, key, etc...)
        DBObject     query      = new BasicDBObject("_ID",<ID or key value>);
        DBObject     update     = new BasicDBObject("$set", new BasicDBObject(key, newValue));

        //These flags will guarantee that you'lls get the updated result
        DBObject     result     = collection.findAndModify(query, null, null, false, update,true, true);

        //Just for precaution....
        if(result == null)
            return false;

        return result.get(key).equals(newValue);
    }

The previous answer pointed me in the right direction, but the code to add a 2nd object to the update did not work for me. The following did:

BasicDBObject newValues = new BasicDBObject("age", 10);
newValues.append("name", "Some Name");
BasicDBObject set = new BasicDBObject("$set", newValues);
collection.update(someSearchQuery, set);

According to the documents, $set is an alise for $addFields , so just use that:

        var iterable = collection.aggregate(Arrays.asList(
                Aggregates.addFields(new Field("foo", "bar"))
        ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM