mongo修改级联属性时,仅使用$匹配时,查询条件必须匹配到唯一的一条记录,不能是多条记录。否则会出现以下错误:
The positional operator did not find the match needed from the query.
位置操作符没有从查询中找到所需的匹配项。
$.xx.下标可以修改某条嵌套属性数据。$[]可以修改所有嵌套属性数据。
比如下面的例子,可以某一个嵌套内容:
db.getCollection('sys_tenant').update(
{"xxx":'222'},
{$set:{"xxx.$.sss.0":''}},
{multi:true}
)
模糊查询
db.getCollection('ddd').find({"sss":/^.*xxx.*$/})
聚合查询(控制台复制后稍作调整->shell)
db.r.aggregate([
{ "$unwind" : "$xxx"},
{ "$project" : { "d1" : 1, "d2" : 1, "s" : "$xxx.s",
"d" : "$xxx.q", "d3" : "$xxx.d3",
"d" : "$xxx.q", "c" : "$xxx.c",
"d" : "$xxx.c", "c" : "$xxx.c",
"s" : "$xxx.c.s",
"t" : "$xxx.c.x"}},
{ "$sort" : { "x" : -1}}
])