0

This more like a "hey is this the way mongoose / mongodb works" as oppose to a how do I do this?

Let me specify though :

Model.find({name:'jim'}, function(err, jim){
    // i should happen first & find 0 jim's 
    new Model({name:'jim'}).save( ... );
});

Model.find({name:'jim'}, function(err, jim){
    // i should happen second & find 1 jim 
    new Model({name:'jim'}).save( ... );
});

I would like to have everything inside of the find to belay any future inserts to that model until the new model is saved. Any thoughts?

Ross The Boss
  • 624
  • 5
  • 17

1 Answers1

0

This is pretty well covered by the mongoose documentation

It would be more like:

Model.findOne({name:"jim"}, function(err, jim) {
  if (err) return handleError(err);

  jim.newField = "new value";
  jim.save(function (err) {
    if (err) return handleError(err);
    res.send(jim);
  });
});

It would not be atomic because you'd be finding 'jim' and then during the execution of the callback function, the 'jim' document could change from some different client's operation.

The documentation I linked to provides other methods of updating a document which would be atomic.

Answer derived from references made in comments:

... data.locked = true;
Model.findOneAndUpdate(query, data, {upsert:true}, function(err, doc){...} );
// in your callback function - you can query against models w/ that 
// locked attribute being true 
//- if your logic is true keep&update record , else **remove**
NoOutlet
  • 1,949
  • 1
  • 14
  • 22
  • Yes, I've read the mongoose documentation. My question was how to accomplish an atomic find... e.g. how can I make it so that everything inside of that find command belays inserts on the same model? – Ross The Boss Jan 30 '15 at 19:41
  • I'm not familiar with the term 'belay' in the context you are using it. The [update](http://mongoosejs.com/docs/api.html#model_Model.update) operation is atomic, but if you need to have access to the document in your application afterward, you might want [findOneAndUpdate](http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate) which is also atomic. – NoOutlet Jan 30 '15 at 19:57
  • Gotcha -- so I actually need to find a set of documents, if they pass some logic , then there should be the insert, else no insert. ... I can't think of a way to use findOneAndUpdate like that. Do you know an alternative? – Ross The Boss Jan 30 '15 at 20:09
  • If there is no way to write the logic that the document has to pass into a query form, then I don't know that it can be done atomically. The other aspect you seem to be interested in sounds more like a [JavaScript synchronous vs asynchronous](http://stackoverflow.com/questions/17181248/making-mongoose-js-queries-run-synchronously) issue. – NoOutlet Jan 30 '15 at 20:18
  • in this case synchronous db calls would actually not solve any problems e.g. 1-someone does lookup, 2-someone does lookup, 1-insert, 2-insert , my context being from a web server. Anyways, my solution was probably more efficient then an atomic find, but is not a general solution. I'm going to mention that **findOneAndUpdate** was very helpful though. – Ross The Boss Jan 30 '15 at 22:50