vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

Update data without using find #170

Open Signorini opened 7 years ago

Signorini commented 7 years ago

Where way to make update document without using find?

I try to

let post = {_id: "fsdfsdasasd", changeme: true}

new Post(post).save()

//or

new Post(post).update()

but dont change

tiagobnobrega commented 7 years ago

Hi Signorini,

I was facing the same problem. I found out that, in my case, all I needed to do was transform the "_id" String representation into a actual ObjectId object. Try this instead:

//import from native driver
const ObjectID = require('mongodb').ObjectID;

//will insert or update
let post = {_id: ObjectID("fsdfsdasasd"), changeme: true}
new Post(post).save()

//... Other stuff happening

//will update
let postAgain = {_id: ObjectID("fsdfsdasasd"), changeme: false}
new Post(postAgain ).save()

By the way... not tested ;)