這個問題與其他類似問題不同,因為 { upsert: true } 選項沒有給出所需的輸出。我想更新/插入以下給定的檔案:
{
_id: {
playerId: '1407081',
tournamentId: '197831',
matchId: '1602732'
},
playerName: 'abcd',
points: -5
}
執行下面給出的代碼后,
await Points.findOneAndUpdate(
{
"_id": playerStatsObjForDB._id
},
{
playerStatsObjForDB
},
{
upsert: true
}
);
//where playerStasObjForDB is same as object mentioned in top.
如果檔案不存在,則插入以下檔案:
{
"_id": {
"playerId":"1407081",
"tournamentId":"197831",
"matchId":"1602732"
},
"__v":0,
}
我正在使用貓鼬,但非常感謝任何幫助。
uj5u.com熱心網友回復:
那么 MongoDB upsert 的作業方式如下:
db.products.updateMany(
{ _id: 6 },
{ $set: {price: 999} },
{ upsert: true}
)
所以基本上第一個引數是查詢本身,就像你所做的一樣,但第二個引數應該是你想要修改的更新,你應該使用像 $set 這樣的 MongoDB 運算子。相反,您似乎沒有修改就將整個物件嵌入其中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350914.html
標籤:节点.js MongoDB 猫鼬 mongodb-查询
