guarantee MongoDB update operation is successful

169 views Asked by At

Maybe a dumb question, how to guarantee your update operation is successful? This is how I did it, but the returned "result" is "false".

import { Mongo } from "meteor/mongo";
const Employees = new  Mongo.Collection("Employees");

const result = Employees.update({_id: employeeId}, {$push: {tasks: taskId}});
console.log(result); // "false"

Thanks

Derek

1

There are 1 answers

0
Kevin Smith On

All depends what you class as successful? If you take a look at WriteConcern you can get results for acknowledgments, write to journal or writes to a number of nodes with the cluster. This will all depend on your application and how it can deal with failures.

As for getting the number of effected rows you can use the call back on the update method (see the API documentation)

callback Function

Optional. If present, called with an error object as the first argument and, if no error, the number of affected documents as the second.