查询比较对象数组 [英] Query Comparing Arrays of Objects

查看:95
本文介绍了查询比较对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何比较Mongoose中的(嵌套)对象数组。

I'm wondering how I can compare arrays of (nested) objects in Mongoose.

考虑到下面的数据,我希望在<$时得到结果c $ c> name 属性匹配。任何人都可以帮我吗?

Considering the data below, I would like to get results when the name properties match. Could anyone help me with this?

    Organisation.find( { 
        $or: [ 
          { "category_list": { $in: cat_list } }, 
          { "place_topics.data": { $in: place_tops } } 
        ] 
      } 
    )

假设这是我MongoDB中存储的数据:

Let's say that this is the data stored in my MongoDB:

  "category_list": [
      {
          "id": "197750126917541",
          "name": "Pool & Billiard Hall"
      },
      {
          "id": "197871390225897",
          "name": "Cafe"
      },
      {
          "id": "218693881483234",
          "name": "Pub"
      }
  ],
  "place_topics": {
      "data": [
          {
              "name": "Pool & Billiard Hall",
              "id": "197750126917541"
          },
          {
              "name": "Pub",
              "id": "218693881483234"
          }
      ]
  }

让我们说这些是我想要的数组比较(几乎相同的数据):

And let's say that these are the arrays I want to compare against (almost the same data):

let cat_list = [
            {
                "id": "197750126917541",
                "name": "Pool & Billiard Hall"
            },
            {
                "id": "197871390225897",
                "name": "Cafe"
            },
            {
                "id": "218693881483234",
                "name": "Pub"
            }
        ]
let place_tops = [
                {
                    "name": "Pool & Billiard Hall",
                    "id": "197750126917541"
                },
                {
                    "name": "Pub",
                    "id": "218693881483234"
                }
            ]


推荐答案

当有多个条件每个数组元素所需的是当你实际使用 $ elemMatch ,实际上需要否则你不匹配正确的元素。

When there are "multiple conditions" required for each array element is when you actually use $elemMatch, and in fact "need to" otherwise you don't match the correct element.

所以要申请多个条件,你宁愿为 $或 而不是 $ in

So to apply multiple conditions, you would rather make an array of conditions for $or instead of shortcuts with $in:

Organizations.find({
  "$or": [].concat(
    cat_list.map( c => ({ "category_list": { "$elemMatch": c } }) ),
    place_tops.map( p => ({ "place_topics": { "$elemMatch": p } }) )
  )
})

但是,如果你带一个退后一步,从逻辑上思考它,你实际上命名了一个属性id。这通常意味着在所有良好实践中,该值实际上是唯一的。

However, if you take a step back and think logically about it, you actually named one of the properties "id". This would generally imply in all good practice that the value is in fact ""unique".

因此,您真正需要做的就是简单地提取这些值并坚持下去使用原始查询表格:

Therefore, all you really should need to do is simply extract those values and stick with the original query form:

Organizations.find({
  "$or": [
    { "category_list.id": { "$in": cat_list.map(c => c.id) } },
    { "place_topics.id": { "$in": place_tops.map(p => p.id) } }
  ]
})

如此简单将值和属性映射为匹配到id值。这是一个简单的点表示法表单通常在每个数组元素具有一个条件时进行测试/匹配。

So simply mapping both the values and the property to "match" onto the "id" value instead. This is a simple "dot notation" form that generally suffices when you have one condition per array element to test/match.

这通常是给定数据的最合理的方法,你应该应用哪一个实际上适合你需要的数据条件。对于多个我们e $ elemMatch 。但如果你不需要多个,因为有一个单一的匹配,那么只需做单数匹配

That is generally the most logical approach given the data, and you should apply which one of these actually suits the data conditions you need. For "multiple" use $elemMatch. But if you don't need multiple because there is a singular match, then simply do the singular match

这篇关于查询比较对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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