mongoose 比较MongoDB中存储为字符串的数字

sxissh06  于 2023-03-03  发布在  Go
关注(0)|答案(1)|浏览(224)

我想检索经验小于5且大于1的人员。我的架构如下所示

let person = new schema({
    isCompleted: { type: Boolean, default: false },
    candidateId: { type: String },
    firstName: { type: String },
    lastName: { type: String },
    mobile: { type: String },
    emailId: { type: String },
    gender: { type: String },
    phone: { type: String },
    secondaryEmail: { type: String },
    parentId: { type: String },
    totalExperience: { type: String },
    relevantExperience: { type: String },
    website: { type: String },
    skypeId: { type: String },
    additionalInfo: { type: String },
    street: { type: String },
    zipCode: { type: String },
    city: { type: String },
    state: { type: String },
    country: { type: String },
    photo: { type: String },
    isActive: { type: Boolean, default: true },
    isDeleted: { type: Boolean, default: false },
}

我写了一个这样的查询。

let data = await Persons.find(
{"$expr" : {
    "$gte" : [{"$toDouble" :"$totalExperience"} , 1],
    "$lte" : [{"$toDouble" :"$totalExperience"} , 5]}})
.sort(dynamic.sort)
.skip(dynamic.skip)
.limit(dynamic.pageSize)
.select(dynamic.projection)
.populate(dynamic.populate);

但我收到错误MongoServerError:无法解析$convert中的数字“”,没有onError值:空字符串
我的数据库中有null或''数据。这就是我得到这个错误的地方。所以我写了另一个查询。

let data = await Persons.find({$expr: {
            $and: [
              {
                totalExperience:{ $exists: true, $nin: [null, ''] }
              },
              {
                $lte: [
                  {
                    $toDouble: "$totalExperience"
                  },
                  5
                ]
              },
              {
                $gte: [
                  {
                    $toDouble: "$totalExperience"
                  },
                  1
                ]
              }
            ]
          }
        });

但我再次收到此错误MongoServerError:无法识别的表达式“$exists”
有谁能告诉我怎样才能解决这个问题吗?先谢了!

sqxo8psd

sqxo8psd1#

{
  $expr: {
    $and: [
      {
        $not: {$in: ["$totalExperience",["","test"]]}
      },
      {
        $gte: [{$toDouble: "$totalExperience"},1]
      },
      {
        $lte: [{$toDouble: "$totalExperience"},5]
      }
    ]
  }
}

相关问题