更新数组mongodb中的多个元素 [英] update multiple elements in array mongodb
本文介绍了更新数组mongodb中的多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当日期大于特定值时,我想将chargeList isDelete属性更新为true.我的架构如下.
I want to update chargeList isDelete property to true when date is greater than a specific value. my schema as follows.
{
"chargeList": [
{
"date": ISODate("2013-06-26T18:57:30.012Z"),
"price": "123",
"isDelete": false
},
{
"date": ISODate("2013-06-27T18:57:30.012Z"),
"price": "75",
"isDelete": false
}
]
}
模式如下.
var ChargeListSchema= new Schema({
date: { type: Date , default: Date.now },
price: { type: String, required: false },
isDelete: { type: Boolean, default: false }
});
var ScheduleChargeSchema = new Schema({
chargeList:[ChargeListSchema]
});
我已经尝试过执行以下代码,但是它只会更新chargeList数组中匹配的第一个元素.
I have tried as following code but it only update the matching first element in chargeList array.
Model.update(
{
"_id": 1,
"chargeList": {
"$elemMatch": {
"date": {
$gt: ISODate("2013-06-26T18:57:30.012Z")
}
}
}
},
{
"$set": { "chargeList.$.isDelete": true }
}
)
推荐答案
您需要使用 $[] all positional operator
更新数组中的多个元素
You need to use $[] all positional operator
to update multiple elements in an array
Model.update(
{ "_id": 1, "chargeList.date": { "$gt": ISODate("2013-06-26T18:57:30.012Z") }},
{ "$set": { "chargeList.$[].isDelete": true } }
)
这篇关于更新数组mongodb中的多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文