我是 MongoDB 的新手,我正在嘗試做一個非常簡單的任務,但是我做錯了。
我想要的是更改行程狀態,但我嘗試了“FindAndUpdate”、“UpdateOne”和“FindByIdAndUpdate”,但它不起作用。
也許它與我的架構有關。我應該為流程創建一個新的架構嗎?
我在 MongoDB 集合中的資料庫條目:
_id: 622c98cfc872bcb2578b97a5
username:"foo"
__v:0
process:Array
0: Object
processname:"bar"
process_status:"stopped"
_id: 6230c1a401c66fc025d3cb88
我當前的架構:
const User = new mongoose.Schema(
{
username: { type: String, required: true },
process: [
{
processname: {
type: String,
},
process_status: {
type: String,
},
},
],
},
{ collection: "user-data" }
);
我當前的代碼:
const startstopprocess = await User.findByIdAndUpdate(
{ _id: "6230c1a401c66fc025d3cb88" },
{ process_status: "started" }
).then(function (error, result) {
console.log(error);
console.log(result);
});
uj5u.com熱心網友回復:
您可以通過這種方式使用位置運算子$:
db.collection.update({
"process._id": "6230c1a401c66fc025d3cb88"
},
{
"$set": {
"process.$.process_status": "started"
}
})
請注意如何使用位置運算子說 mongo “從您在 find 階段找到的物件中,將process_status變數更新為started”
這里的例子
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/447663.html
標籤:javascript 节点.js mongodb 目的 猫鼬
下一篇:升級Swift依賴項的加密演算法
