简体   繁体   English

查询比较对象数组

[英]Query Comparing Arrays of Objects

I'm wondering how I can compare arrays of (nested) objects in Mongoose. 我想知道如何比较猫鼬中(嵌套)对象的数组。

Considering the data below, I would like to get results when the name properties match. 考虑以下数据,当name属性匹配时,我想获得结果。 Could anyone help me with this? 有人可以帮我吗?

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

Let's say that this is the data stored in my MongoDB: 假设这是存储在我的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"
                }
            ]

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. 当每个数组元素都需要“多个条件”时,就是您实际使用$elemMatch ,而实际上是“需要”时,否则您将不匹配正确的元素。

So to apply multiple conditions, you would rather make an array of conditions for $or instead of shortcuts with $in : 因此,要应用多个条件,您宁愿为$or组成条件数组,而不是使用$in的快捷方式:

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

However , if you take a step back and think logically about it, you actually named one of the properties "id" . 但是 ,如果退后一步并对其进行逻辑思考,则实际上是将属性之一命名为“ 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) } }
  ]
})

So simply mapping both the values and the property to "match" onto the "id" value instead. 因此,只需将值和属性映射为“ match”到"id"值即可。 This is a simple "dot notation" form that generally suffices when you have one condition per array element to test/match. 这是一种简单的“点表示法”形式,当每个数组元素都有一个要测试/匹配的条件时,通常就足够了。

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 . 对于“多个”,请使用$elemMatch But if you don't need multiple because there is a singular match, then simply do the singular match 但是,如果因为存在单数匹配而不需要多个,则只需进行单数匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM