question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.update with $rename does not seem to work

See original GitHub issue

note: I’m using 3.8.33, so if this is resolved in 4.x my apologies, I’m not able to upgrade the project I’m working on yet…

I’ve got a field in a collection that I want to rename, and have been able to do it successfully from the mongo cli using the following:

db.agents.update({}, { $rename: { "state": "contactStatus" } }, { multi: true })

However, I want to write this into an update script so it is applied to my staging and production environments when the new version of my app is released.

Since I’m using mongoose, it seems I should be able to use the Model.update method to achieve the same:

AgentModel.update(
  { },
  { $rename: { state: 'contactStatus' } },
  { multi: true },
  function(err, raw) { console.log(err, raw); }
);

This doesn’t seem to have any effect on the data, however, and the value of raw when logged to the console is 0.

Should this update query be supported by Mongoose? I know that the update query is rewritten to { $set: { update } } when overwrite: false (default), but the docs say:

All top level keys which are not atomic operation names

… which I had expected would mean $rename would work as expected.

Not sure if this is related to #3027? No error is being thrown.

Any guidance would be appreciated, and if there’s further debug information I can provide please let me know.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:27 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
vkarpov15commented, Jan 18, 2017

Try doing db.User.update({}, { $rename: { hashedPassword: 'password' } }, { multi: true, strict: false }, function(err, blocks) { }); . I presume hashedPassword is not defined in your schema so mongoose will strip that out by default unless you do strict: false

9reactions
natanavracommented, Apr 25, 2018

Mongoose was deleting the field from my $rename query since I already renamed the field in the Schema. So I used {strict: false}, that way it works with Model.update. I would think this should be the default behavior, since we’re not planning on leaving both fields in the Schema.

P.S. works well with Model.collection.update but that’s deprecated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rename not working - Visual Studio Feedback
Renaming does not work - When I right-click to rename (or use shortcut) the item to rename is highlighted ... This seems to...
Read more >
Keras rename model and layers - python - Stack Overflow
Class Model seem to have the property model.name, but when changing it I get "AttributeError: can't set attribute".
Read more >
Can't rename both files and folders in Visual Studio 2022
Can 't rename both files and folders in Visual Studio 2022. I have read this, but it was old enough to solve the...
Read more >
Create Aliases to Rename Members in the View - Tableau Help
Tip: If Aliases does not appear in the dialog, verify that you are not connected to a published data source and the field...
Read more >
Rename operations should rename indexes, constraints ...
I tried to migrate to a new model and bumped into this one. To fix, I made sure that I renamed my existing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found