DrakaSAN DrakaSAN - 12 days ago 7
Node.js Question

findOneAndUpdate work in mongo shell, but doesn't update with node-mongodb?

I have this function, which search for a document with a given ID (mID), check if another field exist (u) and if not, add the given id (uID).

The code doesn't throw any error, but doesn't update the document either, however, building the query myself from the same field (the two

console.log
) and using them in the mongo shell does work.

The result of the query when used with node-mongodb-native is
{ ok: 1, nModified: 0, n: 0 }


'use strict';

const MongoClient = require('mongodb').MongoClient,
async = require('async'),
uuid = require('node-uuid'),
winston = require('winston'),
logger = new (winston.Logger)({transports: [new winston.transports.Console()]});

let Link = {};

function link(mId, base, uId, callback) {
let filter = {'mId': mId},
update = {'$set': {}};

filter[base] = {'$exists': false};
update['$set'][base] = uId;

logger.info('link update', {filter: filter, update: update});
console.log('db.Link.findOne(' + require('util').inspect(filter) + ');');
console.log('db.Link.findOneAndUpdate(' + require('util').inspect(filter) + ', ' + require('util').inspect(update) + ', {upsert: false, new: true});');
Link.collection('link')
.findOneAndUpdate(
filter,
update,
{
upsert: false,
returnOriginal: false
}
).then((result) => {
logger.info('link update ok', {result: result});
callback();
}).catch((error) => {
logger.error('link update error', {error: error});
callback(new Error(4299));
});
}

async.waterfall([
(callback) => {
MongoClient.connect('mongodb://127.0.0.1/Link').then((db) => {
logger.info('Connected to Link');
Link = db;
callback(null);
}).catch((error) => {
logger.error('init Link', {error: error});
callback(error);
});
},
(callback) => {
let mId = uuid.v4();
logger.info('create');

Link.collection('Link')
.insertOne({'mId': mId})
.then((error) => {
logger.info('create ok')
callback(null, mId);
}).catch((error) => {
logger.error('create error', {error: error});
callback(new Error(4299));
});
},
(mId, callback) => {
link(mId, 'link', uuid.v4(), callback);
}
], (error) => {
if(error) {
logger.error('End', {error, error});
}
logger.info('End');
Link.close();
});


I also tried with
update
and
updateOne
functions, but same result: command work, not the code.

Can anybody explain why a command that work from the shell couldn't work when made from the driver, and why Mongo report it found a document, but don't update it?

node v6.9.1, node-mongodb-native v2.2.11

Edit:

Base document:

{
"_id" : ObjectId("58332c30224fe3273c7b1ba6"),
"mId" : "37286c83-7d81-484d-b62a-310f690cac97"
}


Updated document:

{
"_id" : ObjectId("58332c30224fe3273c7b1ba6"),
"mId" : "37286c83-7d81-484d-b62a-310f690cac97",
"test" : "f7bb9386-eedd-43fe-890a-348cb3a97ed3"
}


Logger output:

info: Connected to Link
info: create
info: create ok
info: link update mId=f8ba93da-3b6d-43f7-9f90-4e345ba04131, $exists=false, link=f882d44d-60a3-4701-b5df-ba493c3b249b
db.Link.findOne({ mId: 'f8ba93da-3b6d-43f7-9f90-4e345ba04131',
link: { '$exists': false } });
db.Link.findOneAndUpdate({ mId: 'f8ba93da-3b6d-43f7-9f90-4e345ba04131',
link: { '$exists': false } }, { '$set': { link: 'f882d44d-60a3-4701-b5df-ba493c3b249b' } }, {upsert: false, new: true});
info: link update ok updatedExisting=false, n=0, value=null, ok=1
info: End


The
init
function connect to mongoDB, the
create
function insert into it a new document with a random
mId
that is passed to the
link
function.
The uId is also created randomly and is also a UUID.

While it should be equivalent, the command printed in the console logs:

db.Link.findOneAndUpdate({ mId: 'f8ba93da-3b6d-43f7-9f90-4e345ba04131',


link: { '$exists': false } }, { '$set': { link: 'f882d44d-60a3-4701-b5df-ba493c3b249b' } }, {upsert: false});

do update the document

Answer

Well, you're using wrong Collection Name

When you insert one you use uppercase Link

Link.collection('Link') <<<<<<<<<<<<<<<<<<<<
    .insertOne({'mId': mId})

When you try to update you are using lowercase link

Link.collection('link') <<<<<<<<<<<<<<<<<
    .findOneAndUpdate(
Comments