使用 mongodb 文档的其他字段显示数组的条件大小 [英] Display the conditionnal size of an array with the others fields of a mongodb document

查看:26
本文介绍了使用 mongodb 文档的其他字段显示数组的条件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 fridges 的集合,我希望每个冰箱中的一些字段匹配一个条件以及这个冰箱中物品的条件大小".

这是我的数据库的一个例子:

db={冰箱":[{_id: 1,项目: [{itemId: 1,名称:啤酒"},{itemId: 2,名称:鸡"}],品牌:博世",尺寸:195,冷却器:真的,颜色:灰色"},{_id: 2,项目: [{itemId: 1,名称:啤酒"},{itemId: 2,名称:鸡"},{itemId: 3,名称:生菜"}],品牌:伊莱克斯",尺寸:200,冷却器:真的,颜色:白色"},]}

我想获得具有这些共同条件(和"条件)的fridges:

  • brand$in [Bosch",Samsung"]
  • color$in [grey",white"]

另外:名称为 $in [beer",lettuce"]

的项目数

最后:删除一些字段,如结果的 sizeitems.

在我们的例子中,例外的输出是:

<代码>{_id:1项目数:1,品牌:博世",冷却器:真的,颜色:灰色"}

说明:我们删除了字段itemssizeitemsNumber 计算items 数组中啤酒和生菜的数量.我们只保留第一台冰箱,它的品牌是博世,而且是灰色的.

这是我目前所拥有的:

db.fridges.aggregate([{$匹配":{$和:[{品牌":{$in: [博世",三星"]}},{颜色":{$in: [灰色",白"]}}]}},{$项目":{物品编号":{$size: "$items";//这是不好的},品牌: 1,冷却器:1,颜色:1}}])

哪个返回我:

<预><代码>[{_id":1,品牌":博世",颜色":灰色",冷却器":真的,物品数量":2}]

计算与 beerlettuce 匹配的项目是我的主要问题.这是一个可执行示例.

提前致谢!

我发现了如何让它发挥作用.谢谢@joe 建议使用过滤器,这确实是解决方案.

这是完整的查询:

db.fridges.aggregate([{$匹配:{$和:[{品牌":{$in: [博世",三星"]}},{颜色":{$in: [灰色",白"]}}]}},{$项目:{物品编号":{$过滤器":{输入":$items",作为":项目",条件":{$in: [$$item.name",[啤酒",生菜"]]}}},品牌: 1,冷却器:1,颜色:1}}])

可运行示例.

I have a collection of fridges and I would like to have some fields from each fridge matching a condition plus the 'conditionnal size' of the items in this fridge.

This is an example of my DB :

db={
  "fridges": [
    {
      _id: 1,
      items: [
        {
          itemId: 1,
          name:"beer"
        },
        {
          itemId: 2,
          name: "chicken"
        }
      ],
      brand:"Bosch",
      size:195,
      cooler:true,
      color:"grey"
    },
    
    {
      _id: 2,
      items: [
        {
          itemId: 1,
          name:"beer"
        },
        {
          itemId: 2,
          name: "chicken"
        },
        {
          itemId: 3,
          name: "lettuce"
        }
      ],
      brand:"Electrolux",
      size:200,
      cooler:true,
      color:"white"
    },
  ]
}

I want to get fridges with these mutuals conditions ('and' condition):

  • brand is $in ["Bosch","Samsung"]
  • color is $in ["grey","white"]

In addition : The number of items with a name $in ["beer","lettuce"]

And finally : Removing some fields like the size and items of the result.

In our example, the excepted output would be :

{
  _id:1
  itemsNumber:1,
  brand:"Bosch",
  cooler:true,
  color:"grey"
}

Explanations : We removed the field items and size, itemsNumber counts the number of beers and lettuce from items array. And we only keep the first fridge its brand is Bosch and it's grey.

This what I have so far :

db.fridges.aggregate([
  {
    "$match": {
      $and: [
        {
          "brand": {
            $in: [
              "Bosch",
              "Samsung"
            ]
          }
        },
        {
          "color": {
            $in: [
              "grey",
              "white"
            ]
          }
        }
      ]
    }
  },
  {
    "$project": {
      "itemsNumber": {
        $size: "$items" // This is not good
      },
      brand: 1,
      cooler: 1,
      color: 1
    }
  }
])

Which returns me :

[
  {
    "_id": 1,
    "brand": "Bosch",
    "color": "grey",
    "cooler": true,
    "itemsNumber": 2
  }
]

Counting the items matching with either beer or lettuce is my main problem. This is an executable example.

Thanks in advance !

解决方案

I found out how to make it work. Thank you @joe for suggesting to use filter this was indeed the solution.

Here is the complete query :

db.fridges.aggregate([
  {
    $match: {
      $and: [
        {
          "brand": {
            $in: [
              "Bosch",
              "Samsung"
            ]
          }
        },
        {
          "color": {
            $in: [
              "grey",
              "white"
            ]
          }
        }
      ]
    }
  },
  {
    $project: {
      "itemsNumber": {
        "$filter": {
          "input": "$items",
          "as": "item",
          "cond": {
            $in: [
              "$$item.name",
              [
                "beer",
                "lettuce"
              ]
            ]
          }
        }
      },
      brand: 1,
      cooler: 1,
      color: 1
    }
  }
])

Runnable example.

这篇关于使用 mongodb 文档的其他字段显示数组的条件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆