[SERVER-51208] New field `$setOnUpdate` will solve some problems Created: 29/Sep/20  Updated: 06/Oct/20  Resolved: 06/Oct/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Vladimir Revenko Assignee: Eric Sedor
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-42084 Easy syntax for $setIfModified behavior Backlog
Related
related to SERVER-13578 add $setOnUpdate to update operation Closed
Participants:

 Description   

Hello! My name is Vladimir Revenko. I am NodeJS developer and active MongoDB user!

Problem

Tools such as Mongoose use the "timestamp" functionality. This makes it difficult in some cases.

If we run without a `timestamps` parameter mongoose does this:

{{db.cats.updateMany({}, { '$set':

{ name: 'Zildjian' }

})}}

MongoDB returns this:
`matchedCount` — (`n` in Mongoose) containing the number of matched documents;
`modifiedCount` — (`nModified` in Mongoose) containing the number of modified documents.

But, if we run with `timestamps` mongoose (with pre-hook) does this:

db.cats.updateMany({}, {
    '$setOnInsert': {
        createdAt: new Date("Sat, 26 Sep 2020 17:56:54 GMT"),
    },
    '$set': {
        updatedAt: new Date("Sat, 26 Sep 2020 17:56:54 GMT"),
        name: 'Zildjian',
    },
})

even if a `name` field is not modified, `updatedAt` field has been modified and MongoDB will update all matched docs and `modifiedCount` (`nModified` in Mongoose) will always be equal to `matchedCount` (`n` in Mongoose).

I think there are other examples, but timestamps are the most obvious example.

Suggestion for improvement

Maybe you should separate the targeting update fields and the companion update fields at the mongodb level?

Current usage works like this:

db.cats.updateMany({}, {
    '$setOnInsert': {
        createdAt: new Date("Sat, 26 Sep 2020 17:56:54 GMT"),
    },
    '$set': {
        updatedAt: new Date("Sat, 26 Sep 2020 17:56:54 GMT"),
        name: 'Zildjian',
    },
})

New usage could be like this:

db.cats.updateMany({}, {
    '$setOnInsert': {
        createdAt: new Date("Sat, 26 Sep 2020 17:56:54 GMT"),
    },
    '$setOnUpdate': {
        updatedAt: new Date("Sat, 26 Sep 2020 17:56:54 GMT"),
    },
    '$set': {
        name: 'Zildjian',
    },
})

Thе `$setOnUpdate` field means what data needs to be updated along with the data updated by the `$set` field.

  • `matchedCount`  = number of matched docs
  • `modifiedCount` = number of modified only by the `$set` field

I think this will add flexibility when working with the database and, at the same time, it will not break the existing code.

Please see discussions with some examples:


With best regards,
Vladimir Revenko

Email: vova@revenko.org
Telegram: @vovarevenko



 Comments   
Comment by Vladimir Revenko [ 06/Oct/20 ]

Thanks for the answer! I have read SERVER-42084, you can close this ticket 

Comment by Eric Sedor [ 30/Sep/20 ]

Hi vova@revenko.org,

We're going to close this ticket as a duplicate of SERVER-42084, but appreciate your well-described request. Would you be willing to add your thoughts to that ticket and watch it for updates?

Note that SERVER-42084 also offers a workaround that may be of benefit for Mongoose and MongoDB 4.2+.

Sincerely,
Eric

Comment by Asya Kamsky [ 29/Sep/20 ]

This appears to be similar if not identical to SERVER-42084

 

 

Generated at Thu Feb 08 05:24:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.