Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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中子文档数组中的2个特定字段值?_Mongodb - Fatal编程技术网

是否有方法匹配mongodb中子文档数组中的2个特定字段值?

是否有方法匹配mongodb中子文档数组中的2个特定字段值?,mongodb,Mongodb,我有以下格式的数据: { "_id" : ObjectId("5f281caf3494701c38711e96"), "WidgetId" : "0233261", "Specs" : [ { "WidgetType" : "A", "AmountLow&

我有以下格式的数据:

{ 
    "_id" : ObjectId("5f281caf3494701c38711e96"), 
    "WidgetId" : "0233261", 
    "Specs" : [
        {
            "WidgetType" : "A",
            "AmountLow" : NumberLong(0), 
            "AmountHigh" : NumberLong(0), 
        }, 
        {
            "WidgetType: "A",
            "AmountLow" : NumberLong(0), 
            "AmountHigh" : NumberLong(500), 
        }, 
        {
            "WidgetType" : "A"
            "AmountLow" : NumberLong(1), 
            "AmountHigh" : NumberLong(1000), 
        }
    ]
}
然而,数据是错误的,因为我不能有一个值
“Specs.AmountLow”=0
“Specs.AmountHigh”>0
,它们应该是0/0或>0/>0

我很难找到具有
“Specs.AmountLow”=0
“Specs.AmountHigh”>0
特定组合的文档。以下是我尝试的两个查询,但没有成功:

尝试1:

db.widgets.find(
    { 
        "Specs.AmountLow" : NumberLong(0), 
        "Specs.AmountHigh" : { 
            "$gt" : NumberLong(0)
        }
    }, 
    { 
        "WidgetId" : 1.0, 
        "Specs.AmountLow" : 1.0, 
        "Specs.AmountHigh" : 1.0
    }
)
只要
AmountLow
为0或
AmountHigh
大于0,上述查询就会发回所有结果,因此在示例数据中,所有数组值都是匹配的,即使没有0/>0值

我下一次试了这个:

db.widgets.find(
    { 
        "$and" : [
            { 
                "Specs.$.AmountLow" : NumberLong(0)
            }, 
            { 
                "Specs.$.AmountHigh" : { 
                    "$gt" : NumberLong(0)
                }
            }
        ]
    }, 
    { 
        "WidgetId" : 1.0, 
        "Specs.AmountLow" : 1.0,
        "Specs.AmountHigh" : 1.0
    }
);
然而,这一次并没有返回任何结果,即使我用0/>0的值确认了数据

如何编写查询以查找
AmountLow=0
AmountHigh>0
的特定子文档组合,以及作为推论,如何仅更新那些记录以使其具有
AmountLow=1

预期结果:

{ 
    "_id" : ObjectId("5f281caf3494701c38711e96"), 
    "WidgetId" : "0233261", 
    "Specs" : [
        {
            "WidgetType: "A",
            "AmountLow" : NumberLong(0), 
            "AmountHigh" : NumberLong(500), 
        }
    ]
}
你可以试试

当您使用时,它将只从数组和数组中返回一个匹配的文档

  • 使用
    $elemMatch
    进行数组元素匹配
  • 在投影中的数组字段名后使用位置


此示例将从数组中返回所有匹配的文档,而不是您可以使用的上述示例

  • 根据条件从数组中获取筛选的文档

db.widgets.find({
  Specs: {
    $elemMatch: {
      AmountLow: 0,
      AmountHigh: {
        $gt: 0
      }
    }
  }
},
{
  _id: 1,
  WidgetId: 1,
  "Specs.$": 1
})
db.widgets.aggregate([
  {
    $addFields: {
      Specs: {
        $filter: {
          input: "$Specs",
          cond: {
            $and: [
              { $eq: ["$$this.AmountLow", 0] },
              { $gt: ["$$this.AmountHigh", 0] }
            ]
          }
        }
      }
    }
  }
])