Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$push and $set in same MongoDB update

I'm trying to use MongoDB's Java driver to make two updates ($set and $push) to a record in the same operation. I'm using code similar to the following:

    BasicDBObject pushUpdate = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital));
    BasicDBObject setUpdate = new BasicDBObject().append("$set", new BasicDBObject().append("endTime", time));
    BasicDBList combinedUpdate = new BasicDBList();
    combinedUpdate.add( pushUpdate);        
    combinedUpdate.add( setUpdate);


    collection.update( new BasicDBObject().append("_id", pageId), combinedUpdate, true, false);

When I combine the $set and $push into the same update via a BasicDBList, I get an IllegalArgumentException: "fields stored in the db can't start with '$' (Bad Key: '$push')".

If I make two separate updates, both pushUpdate and setUpdate produce valid results.

Thanks!

like image 338
HolySamosa Avatar asked Jan 30 '12 21:01

HolySamosa


People also ask

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

How do you update an array of objects in MongoDB?

Update Documents in an ArrayThe positional $ operator facilitates updates to arrays that contain embedded documents. Use the positional $ operator to access the fields in the embedded documents with the dot notation on the $ operator.

What is the difference between update and Upsert in MongoDB?

Upsert is a combination of insert and update (inSERT + UPdate = upsert). We can use the upsert with different update methods, i.e., update, findAndModify, and replaceOne. Here in MongoDB, the upsert option is a Boolean value. Suppose the value is true and the documents match the specified query filter.

What is the difference between $Push and $addToSet?

The DifferencesIf the value already exists in the array, $push will still append the value (resulting in duplicate values). However, $addToSet only appends the value if it doesn't already exist in the array. Therefore, if the value already exists, $addToSet won't append it (it will do nothing).


1 Answers

I don't know Java driver, but do you have to create a list there? What happens if you try this code?

BasicDBObject update = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital));
update = update.append("$set", new BasicDBObject().append("endTime", time));

collection.update( new BasicDBObject().append("_id", pageId), update, true, false);

This should produce the equivalent of

db.collection.update({_id: pageId}, {$push: {values: dboVital}, $set: {endTime: time}});

Whereas your code produces (I suspect) this:

db.collection.update({_id: pageId}, [{$push: {values: dboVital}}, {$set: {endTime: time}}]);
like image 120
Sergio Tulentsev Avatar answered Oct 17 '22 12:10

Sergio Tulentsev