So I ran into a weird issue, which in hindsight makes perfect sense. While
trying to issue an "upsert" command (which is apparently similar to a REPLACE
command in legit-SQL; insert the record if not found, otherwise update it's
values/columns appropriately), it was dying. Here's my flow:
// attempt to open connection; run update-crud operation
__open(function(err, db) {
__getCollection(db, collection, function(collection) {
collection.update(
spec,
obj,
{
upsert: true,
multi: true,
safe: false
},
callback || function(){}
);
});
});
This code follows the following flow:
- Open a connection to mongo
- Grab the collection specified
- Issue an update command
- Pass in a spec object which defines the query/where clause for what should be
- updated if it exists
- Pass in the object values
- Set some options (eg. that an upsert should occur; that all documents matching
- spec should be used; perform check to see if the operation failed.
- Call callback if specified
Seems legit. It all follows the docs a'la the
node-mongodb-native
package on github. After testing, I found the problem was that I was specifying
multi: true
.
In hindsight, it makes sense. In the above logic, I was writing out that if a
matching "record" was found, update it. I was hoping that if one wasn't found,
it should insert it. But how would multi: true
work? This flag can only be
specified when upsert is set to false. Otherwise, it wouldn't know how many
"records" to insert.
Hopefully this saves someone else a little bit of time.