首页 > 解决方案 > 对象或嵌套文档上的 mongoose $regex

问题描述

const AdressSchema = new Schema(
  {
    title: {
      type: String,
      required: true,
    },
    location: {
      type: Object,
      required: true,
    },
})

位置是我想找到任何匹配表达式的地方

我就是这样做的,但返回空结果

async get() {
   return this.Model.find($or: [{ title: { $regex: regex } },{location: { $regex: regex } }])
  }

标签: node.jsmongodbsearch

解决方案


正则表达式匹配字符串。

MongoDB 查询语言运算符是类型敏感的。

type: Object不是字符串。

因此,$regex运算符永远不会匹配 a location


推荐阅读