Update mongoDB document depending on existing fields


#1

I’m updating a document in my collection using the validated-method package (irrelevant for this though).

Before actually calling the myCollection.update(id, { ... }) function, I’d like to access the fields in in that particular document.

example:

if (myCollection(id).myBooleanField == true) 
{
   myCollection.update(id, { ... }) }

It seems trivial, but I’m not sure how to do this server side?


#2

Did you try just fetching the document?

var document = myCollection.findOne(id);
if (document && document.myBooleanField == true) {
  myCollection.update(id, {...});
}