【问题标题】:Mongoose query model where field type is an array字段类型为数组的 Mongoose 查询模型
【发布时间】:2021-04-27 17:27:06
【问题描述】:

我必须将我的课程模型中字段(位置)的模式类型从数组更改为字符串,但是我需要将我以前拥有的所有数据从数组类型更改为字符串,然后进行一些数据操作以获得我想要的需要。新数据被存储为字符串。如何查询所有具有位置字段类型为数组以及邮政编码所在位置的课程,以便进行适当的数据操作?

const coursesWithArrayLocation = await Course.find({zipcode: { $exists: true }});

【问题讨论】:

    标签: node.js database express mongoose


    【解决方案1】:

    尝试$type运算符来匹配特定类型的字段,

    • 通过2种方式匹配类型首先是Number,其次是Alias,其中数组类型Number是4,Alias是“array”
    const coursesWithArrayLocation = await Course.find({
      location: { $type: "array" },
      zipcode: { $exists: true }
    })
    

    Playground

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2014-12-12
      • 2013-02-12
      • 2013-10-23
      • 2019-05-31
      • 1970-01-01
      相关资源
      最近更新 更多