Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more info to docs for update() function #571

Closed
clintharris opened this issue Oct 21, 2011 · 22 comments
Closed

Add more info to docs for update() function #571

clintharris opened this issue Oct 21, 2011 · 22 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation

Comments

@clintharris
Copy link

Please add something like the following to the documentation (here and here) for the update() function:

Be careful to not use an existing model instance for the update clause (this won't work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a "Mod on _id not allowed" error.

The documentation currently only shows update() being used with a modifier like $inc, but it's not immediately clear to newbies (like myself) that you can use it to insert/replace entire documents. Example:

var contact = new Contact({
  phone: request.phone,
  status: request.status
});

// Convert the Model instance to a simple object using Model's 'toObject' function
// to prevent weirdness like infinite looping...
var upsertData = contact.toObject();

// Delete the _id property, otherwise Mongo will return a "Mod on _id not allowed" error
delete upsertData._id;

// Do the upsert, which works like this: If no Contact document exists with 
// _id = contact.id, then create a new doc using upsertData.
// Otherwise, update the existing doc with upsertData
Contact.update({_id: contact.id}, upsertData, {upsert: true}, function(err{...});
@mlegenhausen
Copy link

+1
I had exactly all the problems you described here...

@diogogmt
Copy link
Contributor

+1

@mgodwin
Copy link

mgodwin commented Apr 18, 2012

+1 - Especially mentioning 'upsert' in the actual documentation would be great!

@saifbechan
Copy link

+1 Same problem here. Removing _id from the object did the trick for me. This should be added to the docs.

@aheckmann
Copy link
Collaborator

we'll be rewritting the docs soon.

@dopa
Copy link

dopa commented Jun 15, 2012

+1 - has this been updated?

@aheckmann
Copy link
Collaborator

not yet. v3 alpha is almost out. will start docs afterward

@sandinmyjoints
Copy link

+1

@aheckmann aheckmann reopened this Oct 3, 2012
@agoole
Copy link

agoole commented Nov 21, 2012

V3 is out and I saw that this issue is still open. I'm having issues with update and upsert not using the default values when creating a document when one isn't found. Docs would really help.

@aheckmann
Copy link
Collaborator

The docs were updated in v3. Anyone want to submit a pull request for this? Should be pretty easy.

@LiamKarlMitchell
Copy link

Can we do this in .save if there is an option like update:true.

@LiorZ
Copy link

LiorZ commented Apr 9, 2013

+1

4 similar comments
@enricleon
Copy link

+1

@d0nd3r3k
Copy link

+1

@madhums
Copy link

madhums commented Apr 23, 2013

+1

@RyanQuackenbush
Copy link

+1

aheckmann added a commit that referenced this issue Jul 15, 2013
aheckmann added a commit to mongodbinc-interns/mongoose that referenced this issue Aug 16, 2013
@simonh1000
Copy link

This is great, except that when I try to implement it I am getting an element with _id null and that is being overwritten whenever I am creating new elements. My existing elements have an _id of type ObjectID and can thus be distinguished

@samschooler
Copy link

I'm having the same issue as simonh1000. The _id value is set to null on update.

@simonh1000
Copy link

I now use this coffeescript
id = if item._id is "" then mongoose.Types.ObjectId() else item._id and that works

@samschooler
Copy link

Alright! Thanks! I got mine working now.

@tristan625
Copy link

+1

@vkarpov15 vkarpov15 added the 4.x label Feb 5, 2015
@skoope
Copy link

skoope commented Nov 8, 2017

Man you're a hero thank you ! +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests