5

I have an existing user object in the database.

I've modified my models/user.js to add an attribute "isAdmin" to the User schema.

  , isAdmin     : { type: Boolean, default: false }

But now when I save the document (ie: I update my profile). The isAdmin attribute shows up when I console.log(user) in my app...(it is false as expected)...but when I goto the mongo db console directly, it is not present in the existing document.

If I create a new user object, the isAdmin attribute IS present in the new doc. How can I add this attribute to an existing document so that it shows up in the db directly?

edit: i've decided to add a role attribute and just modify the particular user setting their role to admin in robomongo instead of using mongoose to do it.

3
  • 2
    Just out of curiosity, Why do you want all the documents to have the new attribute? Part of the appeal of a document database like MongoDB is the fact that not all the documents need to have the same structure. I would rather take advantage of this than "fight" against it. Commented Jan 31, 2013 at 14:38
  • I defined the schema and saved the user object, expecting to be able to manually go into the database and set the isAdmin flag to true for my account. But that attribute didn't show up. Even though when I dump the object using mongoose, it is there set to false. That was confusing me. Commented Feb 1, 2013 at 7:19
  • hey @chovy i have the same problem, infact i am also trying to add a new attribtue to the mongoose models defined, example , as you are have added a new attrubute isAdmin, to the model, then only the new user will have the isAdmin property in the collection right?, and while handling the requests we lack the isAdmin property with the old users.., how will you manage this problem?, you might argue that, the old users would be getting a property of false, but i dont think so, i have to try it.., did anyone try? Commented Dec 4, 2020 at 7:39

2 Answers 2

1

I agree with Hector regarding writing your code to treat a missing isAdmin field the same as it being set to false instead of bothering with this.

But if you wanted it done, one way would be to explicitly mark isAdmin as modified so that the subsequent call to save would write out the default value:

doc.markModified('isAdmin');
doc.save();
Sign up to request clarification or add additional context in comments.

1 Comment

how will it work, what is the doc here?, infact he is trying to add a new attribtue to the mongoose models defined, example , he has added a new attrubute isAdmin, to the model, then only the new user will have the isAdmin property in the collection right?, and while handling the requests we lack the isAdmin property with the old users.., how will you manage this problem?, you might argue that, the old users would be getting a property of false, but i dont think so, i have to try it.., did anyone try?
0

just update the existing documents in mongo shell to add the new property.

6 Comments

Actually, I was a little worried that adding an attribute manually w/o using Mongoose might cause some conflict.
oh i see. but unless this persisting framework have some schema option like ifNull:false, i dont think it will add missing attribute to your document(s).
I ended up adding a user.role attribute and just manually update to equal user.role = 'admin' for my admin users using RoboMongo
this isn't plausible for production applications, especially those with a significant number of records, where running this type of query would take an inordinate amount of time
@Hypermattt maybe a hashed index will help, if the case is find({ isAdmin: true })
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.