0
votes

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' } } )
2
1. doc says: "If the field does not exist, $inc sets the field to the specified amount." so you are right / I dont get the rename thing, can you explain in more detail? - joschua011
see my updated question - quarks
there is no upsert for rename queries? right? - quarks

2 Answers

1
votes

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 :)

0
votes

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.