我知道這可能是一個重復的問題,但現有的答案都沒有解決我的問題。
我有一個friendsSchema如下:
const friendsSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
},
friends: [{
friendId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
},
isFromFacebook: {
type: Boolean,
default: false
},
thisFriendReceivedGiftTimeStamp: {
type: Date,
default: null
},
thisFriendSentGiftTimeStamp: {
type: Date,
default: null
}
}]
}, {
timestamps: true
});
客戶正在向我發送一個friendsIds.
所以,我想在friends陣列中找到所有匹配的所有物件friendsIds并將它們更新thisFriendReceivedGiftTimeStamp為Date.now().
考慮客戶端正在以陣列形式發送 100 個 id friendsIds,什么是實作結果的最有效方式。
提前致謝。
uj5u.com熱心網友回復:
db.collection.update({IF YOU WANT TO ADD QUERY ACCORDING TO OWNER FIELD PLEASE ADD HERE},
{
"$set": {
"friends.$[x].thisFriendReceivedGiftTimeStamp": new Date()
}
},
{
"arrayFilters": [
{
"x.friendId": {
$in: [
1,
3,
5
]
}
}
],
"multi": true
})
arrayFilters在這里應該很好用。但我不是 100% 確定效率
這是一個有效的mongoplayground 鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481730.html
標籤:javascript 猫鼬
上一篇:我將如何用貓鼬選擇許多物件?
