![](/img/trans.png)
[英]How to find and update nested array element in an object on mongodb
[英]Find and update of object in nested array
我有這個mongoose架構
{
id: mongoose.Schema.Types.ObjectId,
name: String,
initialMailSended: Boolean,
email: String,
users: [{
id: mongoose.Schema.Types.ObjectId,
name: String,
surname: String,
email: String,
signedUp: Boolean,
company: String,
email: String,
attending: String,
accomod: String,
program: String,
food: String,
other: String,
allFood: String,
form: [{
name: String,
value: String,
}]
}]
}
我需要做的是編寫一個mongoose查詢,通過id找到這個對象,在這個對象中查找用戶並更新它。 我嘗試了各種查詢,但沒有找到任何可行的查詢。
對於您的問題,您可以像這樣更新您的用戶
db.getCollection('collection').findOneAndUpdate(
{
_id:ObjectId("5cb19dbc85fdcb7868400107"), // collection id
'users._id':ObjectId("5cb19dbc85fdcb7868400185") // user id inside collection
},
{$set: {'users.$.name': 'new value'}} // set name field
)
也看到這個問題用MongoDB更新嵌套數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.