定义
带有过滤器的位置操作符$<identifier>定义数组中数据更新时,只符合identifier定义条件的元素才可以更新。其中<identifier>作为查询条件,需要在arrayfilters中定义。语法如下
{<update operator>: { "<array>.$<identifier>": value}},
{ arrayfilters: [{<identifier>: <condition>}]}搭配使用arrayfiters,更新数组元素中符合过滤条件的数组。
db.collection.updatemany(
{ <query conditions>},
{ <update operator>: { "<array>.$[<identifier>]": value}},
{ arrayfilters: [{<identifier>: <condition>}]}
)其中identifier,是一小写字母开头的字符串,只能包含小写字母和数字
行为
- 自mongodb5.0开始,update操作按照字段名称的字典顺序更新字段。当字段中包含数字时,按照数字顺序依次更新字段。当然,对一个文档的多个字段操作,是原子性的。
- 在arrayfilters中,不能包含文本索引操作符$expr, $text, $where
- 当使用{upsert: true}并产生insert行为时,数据查询过滤条件必须包含一个针对操作数组字段的精确查询。这样才可以在update语句中,使用$[identifier]操作符 。按照下面的更新语句,来定义查询和使用$[identifier]操作符。
db.collection.update(
{myarray: [5,8]},//查询过滤条件中必须包含myarray数组
{$set: {"myarray.$[element]": 10}},//更新字段名称为myarray的数组中所有元素
{upsert: true, arrayfilters: [{element: 0}]}
)当没有文档符合查询条件时,该操作向数据库中插入一份新的文档。
{ "_id": objectid(...), "myarray": [10, 10]}当数据更新操作中没有包含该数组的精确查询时,若数据集中不包含符合查询条件的数据记录,更新插入操作会报错。下面的两个数据更新操作会报错。
db.emptycollection.updateone(
{},
{ $set: {"myarray.$[element]": 10}},
{ upsert: true, arrayfilters: [{element: 9}]}
)
writeerror({
"index" : 0,
"code" : 2,
"errmsg" : "the path 'myarray' must exist in the document in order to apply array updates.",
"op" : {
"q" : {
},
"u" : {
"$set" : {
"myarray.$[]" : 10
}
},
"multi" : false,
"upsert" : true
}
})- $[]操作符可以用在多层嵌套数组当中。
应用
更新符合arrayfilters条件的数组元素
向students表插入数据。其中grades字段是数字类型的数组。
db.students.insertmany([
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
])构建数据更新语句,将大于100分的数据更新为100.
db.students.updatemany(
{},
{ $set: {"grades.$[element]": 100}},
{ arrayfilters: [{"element": {$gte: 100}}]}
)更新数组中符合arrayfilter条件的文档
使用$[identifier]操作符,能够更新数组内包含的文档字段值。$[identifier]操作符与点操作符一起,指定数组内文档元素字段名称,实现字段值的更改。
db.collection.updateone(
{<query selector>},
{ <update operator>: {"array.$[identifier].field":value}}
{ arrayfilters: [{<identifier>: <condition>}]}
)构建students2 集合,其中grades是文档型数组。
db.students2.insertmany([
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}])更新符合多个查询条件的数组元素
向students3集合插入数据
db.students3.insertmany([
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 100, "std" : 6 },
{ "grade" : 87, "mean" : 100, "std" : 3 },
{ "grade" : 85, "mean" : 100, "std" : 4 }
]
}
])构建数据更新语句,要求将grades数组中,grade大于等于30同时std大于等于5的元素std值减少1
db.students3.updatemany(
{},
{$inc: {"grades.$[elem].std": -1}},
{arrayfilters: [{"elem.grade": {$gte: 80}, "elem.std": {$gte: 5}}]}
)使用反向操作符查询更新数组中的文档元素
向集合alumni插入数据
db.alumni.insertmany([{
"_id": 1,
"name": "christine franklin",
"degrees": [
{ "level": "master" },
{ "level": "bachelor" }
],
},
{
"_id": 2,
"name": "reyansh sengupta",
"degrees": [ { "level": "bachelor" } ],
}
])构建数据更新语句,要求将degrees数组中level=bachelor以外的元素,添加字段gradcampaign,并将字段值设置为1.
db.alumni.updatemany(
{},
{$set: {"degrees.$[degree].gradcampaign": 1}},
{ arrayfilters: [ {"degree.level": {$ne: "bachelor"}}]}
)与$[]集合使用更新数组内嵌套数组元素值
向集合students4插入数据。其中grades是文档数组。文档的questions字段是数字数组类型。
db.students4.insertone(
{ "_id" : 1,
"grades" : [
{ type: "quiz", questions: [ 10, 8, 5 ] },
{ type: "quiz", questions: [ 8, 9, 6 ] },
{ type: "hw", questions: [ 5, 4, 3 ] },
{ type: "exam", questions: [ 25, 10, 23, 0 ] },
]
}
)构建数据更新语句,要求更新grades数组中,type字段值为quiz的文档,将questions数组中大于等于8的元素值增加2.
db.students4.updatemany(
{},
{ $inc: {"grades.$[t].questions.$[score]": 2}},
{arrayfilters: [{"t.type": "quiz"}, {"score": {$gte: 8}}]}
)其中,$[t]和$[score]中括号当中,不需要添加空格,否则mongodb会报错 。
writeerror({
"index" : 0,
"code" : 2,
"errmsg" : "no array filter found for identifier ' t ' in path 'grades.$[ t ].questions.$[score]'",
"op" : {
"q" : {
},
"u" : {
"$inc" : {
"grades.$[ t ].questions.$[score]" : 2
}
},
"multi" : true,
"upsert" : false,
"arrayfilters" : [
{
"t.type" : "quiz"
},
{
"score" : {
"$gte" : 8
}
}
]
}
})构建数据更新语句,要求更新grades数组中的所有文档,将questions数组中大于等于8的元素值增加2.
{
"acknowledged": true,
"matchedcount": 1,
"modifiedcount": 1
}以上就是mongodb使用$<identifier>过滤更新数组元素的示例代码的详细内容,更多关于mongodb $<identifier>过滤元素的资料请关注代码网其它相关文章!
发表评论