0

Ran this via terminal on an existing mongodb database hosted in aws ec2 instance:

db.users.update({},{ $inc: { point_total: 100 } }, {multi:true})

In additional to increasing variable by 100 for all users there were also entry changes to another variable (history) in the same collection.

history: [{
    c_id: String,
    created_at: {type: Date, default: Date.now },
    reason: String, //Received, Sent
    amount: String
}]

Unwanted changes include the reason variable within history was changed from "Received" to "Sent" for various users.

As my query wasn't addressing "history", are there ideas as to what caused this?

Alexandra G
  • 61
  • 1
  • 3
  • Your update query is not related to changes in history field. Also you didn't specify what kind of changes in history you have. So it's impossible to guess what happened – Sergey Berezovskiy Mar 28 '17 at 23:08
  • @SergeyBerezovskiy - Unwanted changes include the reason variable within history was changed from "Received" to "Sent" for various users. – Alexandra G Mar 28 '17 at 23:16

1 Answers1

2

From $inc operator documentation:

The $inc operator increments a field by a specified value and has the following form: { $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }

Behavior

If the field does not exist, $inc creates the field and sets the field to the specified value. Use of the $inc operator on a field with a null value will generate an error. $inc is an atomic operation within a single document.

As you can see, the definition states that $inc operator increments only specified fields. There are no any remarks on possible side effects and changing non-specified fields.

That means there is another query (or queries) which update history field. Possibly your application or some service is doing that.

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Yes, I've read the documents on queries. I only ran that one query above manually in terminal. Then I restarted PM2 for nodejs server. Wondering if this was the cause. Or if the "{multi:true}" is part of the issue. I also was wondering if there are issues I'm not understanding properties with subdocuments in arrays. – Alexandra G Mar 29 '17 at 19:15