以前はmongoコレクションにドキュメントを挿入していたと言えます。ノードのMongoDBでfindOneAndUpdateを使用する方法
MongoClient.connect(url, function(err,db){
if(err) {throw err;}
else {
document = {action: "alert",
protocol: "udp",
port: "80",
_id: "12" }
var collection = db.collection("connections");
collection.insertOne(document, function(err,result){
if (err) {throw err;}
else {
console.log("Successful")
db.close();
}
}
}
ここで、プロトコルフィールドを更新します。これまでのところ運がないものは
MongoClient.connect(url, function(err,db){
if (err) { throw err; }
else {
var collection = db.collection("connections");
collection.findOneAndUpdate({_id: "12"}, {$set: {protocol: "http"}}, {new: true}, function(err,doc) {
if (err) { throw err; }
else { console.log("Updated"); }
});
}
});
私は間違ったパラメータをfindOneAndUpdateメソッドに渡していますか?私はデータベースに正しく接続します。
実際に_idを「12」と定義していますか?または、あなたはMongoのObjectIdを受け入れていますか?後者の場合、ObjectIdインスタンスではなくクエリ内の_idとして文字列を渡すので、ドキュメントを見つけることができません。また、エラーはどうなっていますか?最後に、なぜ '{new:true}'? –
あなたはMongoose(http://mongoosejs.com/index.html)など何かを使っていますか?普段はnode.jsに人々が使用しているものですエラーメッセージがありますか? – kazu
私はノードにmongodbを使用しています。また、正しく更新されるため_idエラーではなく、ドキュメント全体をプロトコルのみに置き換えます。 – Spothedog1