Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:根据其他对象设置集合中的值 问题_Mongodb - Fatal编程技术网

MongoDB:根据其他对象设置集合中的值 问题

MongoDB:根据其他对象设置集合中的值 问题,mongodb,Mongodb,文档如下所示: { data: [ { identifier: 'a', value: 10 }, { identifier: 'b', value: 53 }, { identifier: 'c', value: 16 } ] } 如果标识符为x的同一数组中的另一个对象满足条件,我想将所有值设置为null。现实生活中的例子: 如果标识符为a的对象的属性value大于9,则将标识符为b或c的对象的属性值设置为null 方法 我正在尝试使用$project管道,但

文档如下所示:

{
  data: [
    { identifier: 'a', value: 10 },
    { identifier: 'b', value: 53 },
    { identifier: 'c', value: 16 }
  ]
}
如果标识符为
x
的同一数组中的另一个对象满足条件,我想将所有值设置为
null
。现实生活中的例子:

如果标识符为
a
的对象的属性
value
大于
9
,则将标识符为
b
c
的对象的属性
值设置为
null

方法 我正在尝试使用
$project
管道,但不知道如何找到其他对象

// ... Rest
{
$unwind: '$data'
}, {
$project: {
  data: {
    identifier: 1,
    value: {
      $cond: {
        if: {}, // Implement
        then: null, // Only valid for current value
        else: '$row.value'
      }
    }
  }
}
// ... Rest
想要的产出 结果聚合应该是一个数组

如果
a
的值符合以下条件:

[
  { data: { identifier: 'a', value: null } },
  { data: { identifier: 'b', value: null } },
  { data: { identifier: 'c', value: null } },
  // ... more documents
]
[
  { data: { identifier: 'a', value: 10 } },
  { data: { identifier: 'b', value: 53 } },
  { data: { identifier: 'c', value: 16 } },
  // ... more documents
]
如果
a
的值不符合以下条件:

[
  { data: { identifier: 'a', value: null } },
  { data: { identifier: 'b', value: null } },
  { data: { identifier: 'c', value: null } },
  // ... more documents
]
[
  { data: { identifier: 'a', value: 10 } },
  { data: { identifier: 'b', value: 53 } },
  { data: { identifier: 'c', value: 16 } },
  // ... more documents
]

尝试此聚合,而不使用
$unwind

db.col.aggregate([
    {$addFields : {
        index : {$indexOfArray : ["$data.identifier", "a"]},
        isTrue : {$gte : [{$arrayElemAt : ["$data.value", {$indexOfArray : ["$data.identifier", "a"]}]}, 10]}
        }},
    {$addFields : {
        row : {
            identifier : {$arrayElemAt : ["$data.identifier", "$index"]},
            value : {$cond : ["$isTrue", {$arrayElemAt : ["$data.value", "$index"]}, null]}
        }
    }},
    {$project : {index :0, isTrue : 0, data : 0}}
]).pretty()
结果

> db.col.aggregate([ {$addFields : { index : {$indexOfArray : ["$data.identifier", "a"]}, isTrue : {$gte : [{$arrayElemAt : ["$data.value", {$indexOfArray : ["$data.identifier", "a"]}]}, 10]} }}, {$addFields : { row : { identifier : {$arrayElemAt : ["$data.identifier", "$index"]}, value : {$cond : ["$isTrue", {$arrayElemAt : ["$data.value", "$index"]}, null]} } }}, {$project : {index :0, isTrue : 0, data : 0}} ]).pretty()

{ "_id" : 1, "row" : { "identifier" : "a", "value" : 10 } }
> 
{
    "data" : [
        {
            "identifier" : "a",
            "value" : 10
        },
        {
            "identifier" : "b",
            "value" : 53
        },
        {
            "identifier" : "c",
            "value" : 16
        }
    ]
}
编辑

使用
$map
将值设置为所有标识符的值

db.col.aggregate([
    {$addFields : { 
        isTrue : {$gte : [{$arrayElemAt : ["$data.value", {$indexOfArray : ["$data.identifier", "a"]}]}, 10]}
    }},
    {$addFields : {
        data : 
            {$map : {
                input : "$data",
                as : "d",
                in : {
                    identifier : "$$d.identifier",
                    value : {$cond : ["$isTrue", "$$d.value", null]}
                }
            }}
    }},
    {$project : {_id :0, isTrue : 0}}
]).pretty()
结果

> db.col.aggregate([ {$addFields : { index : {$indexOfArray : ["$data.identifier", "a"]}, isTrue : {$gte : [{$arrayElemAt : ["$data.value", {$indexOfArray : ["$data.identifier", "a"]}]}, 10]} }}, {$addFields : { row : { identifier : {$arrayElemAt : ["$data.identifier", "$index"]}, value : {$cond : ["$isTrue", {$arrayElemAt : ["$data.value", "$index"]}, null]} } }}, {$project : {index :0, isTrue : 0, data : 0}} ]).pretty()

{ "_id" : 1, "row" : { "identifier" : "a", "value" : 10 } }
> 
{
    "data" : [
        {
            "identifier" : "a",
            "value" : 10
        },
        {
            "identifier" : "b",
            "value" : 53
        },
        {
            "identifier" : "c",
            "value" : 16
        }
    ]
}

您可以尝试下面的聚合查询

使用expression with验证输入条件是否与数组中的任何元素匹配,并输出长度以进行大于0的比较;如果是,则使用将所有值映射为null;如果否,则保持数据值不变

 db.colname.aggregate({"$addFields":{
  "data":{
    "$cond":[
      {"$gt":[
        {"$size":{
          "$filter":{
            "input":"$data",
            "as":"d",
            "cond":{
              "$and":[
                {"$eq":["a","$$d.identifier"]}, 
                {"$gt":["$$d.value", 9]}
              ]
            }
          }
        }}, 
        0
      ]}, 
      {"$map":{
        "input":"$data",
        "as":"d",
        "in":{ "identifier": "$$d.identifier", "value": null }
      }}, 
      "$data"
    ]
  }
}})

不,它应该保持原样。因此,输入和输出基本相同,但是
可能是
null
。完全正确!谢谢,其实不是。我很抱歉。结果应该是一个数组。我更新了它!对不起,我的例子不完整。结果应该是一个包含所有文档的数组。我更新了这个例子@Julian更新了答案,以更新所有看起来有希望的标识符值。我需要
$unwind
以供以后的
$group
使用,但我可以在之后使用。您能解释一下这两种方法(在“编辑”之前和之后)的区别吗?使用
$map
的解决方案在我看来更好。编辑前只返回匹配的行,编辑后我们使用
$map
根据条件修改所有行值谢谢!我现在正在使用它,但使用的是
$project
而不是
$addFields
,因此我不需要添加新字段。