【问题标题】:How to filter based on a property of an inner object in mongoose?如何根据猫鼬中内部对象的属性进行过滤?
【发布时间】:2021-10-04 02:16:55
【问题描述】:
    const collection = [
      {
        inner_obj: {
          prop: "A"
        }
      }
    ]

    collection.find(
      //code to find outer records based on the `prop` property of column `inner_obj`
    )

如何根据inner_obj列的prop属性查找外部记录。

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    您可以使用Array.filter 过滤掉数组中不符合条件的项:

    const desiredProp = "A"
    
    const collection = [{
      inner_obj: {
        prop: "A"
      }
    },
    {
      inner_obj: {
        prop: "B"
      }
    }]
    
    const filtered = collection.filter(e => e.inner_obj.prop == desiredProp)
    
    console.log(filtered)

    【讨论】:

      【解决方案2】:

      你可以试试这个

      collection.find({"inner_obj.prop": "A"})
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      相关资源
      最近更新 更多