2016-11-18 199 views
0

我有这个功能,它搜索具有给定ID(mID)的文档,检查是否存在另一个字段(u),如果没有,添加给定的id(uID)。findOneAndUpdate在mongo shell中工作,但不使用node-mongodb更新?

该代码不会引发任何错误,但不会更新文档,但是,自己从相同的字段(两个console.log)构建查询,并在mongo shell中使用它们确实有效。

淋巴结mongodb的本地使用时,该查询的结果是{ 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(); 
}); 

我也试图与updateupdateOne功能,但同样的结果:命令的工作,而不是代码。

任何人都可以解释为什么从驱动程序生成的shell命令无法工作,为什么Mongo报告它找到了一个文档,但不更新它?

节点v6.9.1,节点mongodb的天然v2.2.11

编辑:

Base文档:

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

更新文件:

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

记录器输出:

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 

init函数传递给link功能的随机mId连接到MongoDB中,该create功能插入到一个新的文件。 uId也是随机创建的,也是一个UUID。

虽然它应该是等效,在控制台日志打印的命令:

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

链路:{ '$存在':假}},{ '$集':{链接:“f882d44d-60A 3 -4701-b5df-ba493c3b249b'}},{upsert:false});

做更新文档

+0

您不会显示您要更新的文档,但我的猜测是'mId'和'uId'是文档中的数字,但是是代码中的字符串。如果是这样,你需要使用像'{mId:parseInt(mId,10)}'这样的代码进行转换。 – JohnnyHK

+0

@JohnnyHK:不,我只保存UUID(由node-uuid生成),所以一切都是字符串 – DrakaSAN

+0

@DrakaSAN - 告诉我们,什么是记录器输出。我们现在知道,参数是什么,probalby有一些错误。 – libik

回答

0

好,当您插入一个使用大写链接

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

当您尝试更新你,你错了使用集合名称

正在使用小写字母链接

Link.collection('link') <<<<<<<<<<<<<<<<< 
    .findOneAndUpdate(
+0

100pts是一个刚刚没有看到这样一个小错误,对你很好的惩罚的惩罚。 – DrakaSAN

+0

谢谢..很高兴帮助... –

相关问题