私はmongodbでNode.jsを使用しています。findAndModifyを使用してデータベースを更新できません
var mongo = require("mongodb").MongoClient;
var url = process.env.MLAB_URI;
mongo.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established');
var sequence = db.collection('sequence')
//Find and modify the sequence counter.
var obj = sequence.findAndModify({
query: {_id: 1},
update: { $inc: {seq: 1}},
new: true
});
console.log(obj);
sequence.find({_id:1}).toArray(function(err,res){
if(err)
console.log(err);
else
console.log(res)
});
db.close();
}
});
しかし、上記のコードは、データベースを更新していないと返されobj
の出力がPromise { <pending> }
です。 完全な出力は、私は値を更新し、オブジェクトを取得する必要が
Connection established
Promise { <pending> }
[ { _id: 1, seq: 0 } ]
です。それを行う方法はありますか? ありがとうございます!
への変更を意味していますか? seqをインクリメントしようとしています。それは価値を増やしたようです。 – notionquest
はい、以前は14歳でした。 updateコマンドだけで動作します。 –