quarks
quarks

Reputation: 35276

Using MongoDB Update upsert

There are like 5 update operators: $inc, $rename, $setOnInsert, $set, and $unset

When doing a update with upsert option set to true, what will be the value of a newly created document (considering that it does not exist, thus, upsert it), for a field with this update query:

{$inc: {age: 1}}

So the newly inserted document will have the value 1? since It does not exist before. Am I right?

If the operator is $rename, the what would be the value of the field, null? Or the field will not be created as part of the new document to be inserted?

Update:

If the update query is a rename update:

db.students.update( { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } } )

Upvotes: 0

Views: 415

Answers (2)

Felix Yan
Felix Yan

Reputation: 15259

I did the following steps in RockMongo, which should answer your question about $rename:

  1. In a new db/collection, insert a document {"a": 1}
  2. Update it with "$rename": {"b": "c"}
  3. It says "1 rows may be affected."
  4. Come back to the collection, there's still only one data: {"a": 1} (_id omitted). So yes, it just does nothing.

You may just try it yourself :)

Upvotes: 1

Dek Dekku
Dek Dekku

Reputation: 1461

From the docs.

When renaming a single field and the existing field name refers to a non-existing field, the $rename operator does nothing.

When renaming multiple fields and all of the old field names refer to non-existing fields, the $rename operator does nothing.

When renaming multiple fields and some but not all old field names refer to non-existing fields, the $rename operator performs the following operations:

  • Renames the fields that exist to the specified new field names.
  • Ignores the non-existing fields.

Upvotes: 0

Related Questions