対処方法や管理方法がわからない奇妙なエラーがあります。
問題は、関数 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();
}
}
);
});