Burak
Burak

Reputation: 5764

I cannot upsert in Mongoose for Node.js

My schema is:

var VenueSchema = new Schema({
    _id: Schema.Types.ObjectId
    ,rating : Number
})

And I am trying:

var v = new Venue()
v.name = venue.name
Venue.update({ id : Schema.Types.ObjectId(venue.id)}, v, {upsert: true})  

But there is nothing in the DB. Where am I wrong?

Upvotes: 0

Views: 481

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

You need to use _id instead of id and a plain JS object in the update call, and Mongoose will do the ObjectId casting for you. Try this instead:

Venue.update({ _id : venue.id}, {name: venue.name}, {upsert: true});

Note that name doesn't appear in your schema, which probably isn't what you want.

Upvotes: 3

Related Questions