我有這個功能,它搜索具有給定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();
});
我也試圖與update
和updateOne
功能,但同樣的結果:命令的工作,而不是代碼。
任何人都可以解釋爲什麼從驅動程序生成的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});
做更新文檔
您不會顯示您要更新的文檔,但我的猜測是'mId'和'uId'是文檔中的數字,但是是代碼中的字符串。如果是這樣,你需要使用像'{mId:parseInt(mId,10)}'這樣的代碼進行轉換。 – JohnnyHK
@JohnnyHK:不,我只保存UUID(由node-uuid生成),所以一切都是字符串 – DrakaSAN
@DrakaSAN - 告訴我們,什麼是記錄器輸出。我們現在知道,參數是什麼,probalby有一些錯誤。 – libik