我有一些奇怪的错误,我不知道如何治疗或管理。
问题是,有时当我尝试使用函数 findAndModify 更新字段时,它会返回 null。
我尝试调试,看起来两个请求都是一样的,只是结果不同。
//THIS NOT
{ database: 'testdb',
collection: 'mytable',
query: { _id: 5612454f228bd5b7428b456a },
update: { '$inc': { int_lastid: 1 } },
options:
{ new: true,
fields: {},
safe: true,
connection:
EventEmitter {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
socketOptions: [Object],
id: 0,
connected: true,
domainSocket: false,
minWireVersion: 0,
maxWireVersion: 3,
maxBsonSize: 16777216,
maxMessageSizeBytes: 48000000,
maxNumberOfDocsInBatch: 2000,
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null,
eventHandlers: [Object],
maxBsonSettings: [Object],
socketTimeoutMS: [Getter/Setter],
logger: [Object],
connection: [Object],
writeSteam: [Object],
maxWriteBatchSize: 1000,
serverCapabilities: [Object] },
serializeFunctions: false,
checkKeys: false } }
//THIS OK
{ database: 'testdb',
collection: 'mytable',
query: { _id: 5612454f228bd5b7428b456a },
update: { '$inc': { int_lastid: 1 } },
options:
{ new: true,
fields: {},
safe: true,
connection:
EventEmitter {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
socketOptions: [Object],
id: 0,
connected: true,
domainSocket: false,
minWireVersion: 0,
maxWireVersion: 3,
maxBsonSize: 16777216,
maxMessageSizeBytes: 48000000,
maxNumberOfDocsInBatch: 2000,
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null,
eventHandlers: [Object],
maxBsonSettings: [Object],
socketTimeoutMS: [Getter/Setter],
logger: [Object],
connection: [Object],
writeSteam: [Object],
maxWriteBatchSize: 1000,
serverCapabilities: [Object] },
serializeFunctions: false,
checkKeys: false } }
请求过多会影响结果吗?
我的代码是下一个
app.post('/findAndModify', function(req,res){
res.append('Content-Type','application/json');
if(req.body == undefined){
res.status(400);
res.json({"status":400,"message":"Bad request."});
res.end();
return;
}
if(req.body.database == undefined || req.body.collection == undefined || req.body.query == undefined, req.body.update == undefined){
res.status(400);
res.json({"status":400,"message":"Bad request."});
console.log(req.body);
res.end();
return;
}
_database = req.body.database;
_collection = req.body.collection;
_query = req.body.query;
_update = req.body.update;
_options = {};
if(req.body.options != undefined)
_options = req.body.options;
//console.log(_options);
db = req.mongo.getConnection(_database);
collection.findAndModify(
{
"query":_query,
"update": _update,
},_options,function(err,doc){
if(err)
{
res.status(500);
res.json({"status":500, "message":err});
res.end();
}
else{
res.status(200);
if(doc == null){
console.log("//THIS NOT");
console.log(util.inspect(req.body));
}
else{
console.log("//THIS OK");
console.log(util.inspect(req.body));
}
res.json(doc);
res.end();
}
}
);
});