首页 > 解决方案 > MongoDB $gt/$lt 运算符,数据类型为数组,键值对为字符串。如何比较数组中字符串类型的负值?

问题描述

我正在尝试在 MongoDB Compass 和 Robo 3T 上执行查询,以获取大于/小于用户指定数字的值。在我的数据库中,值的存储方式如下:

 "Attributes":
     [{
        "Key": "Score",
        "Value": "-25"
    },
    {
        "Key": "Country",
        "Value": "India"
    }]

我尝试了以下查询,但没有得到结果。不考虑负号进行比较。

用户指定号码:-27

机器人 3T:

1. db.getCollection('Resource').find({ Attributes: { $elemMatch: { Key:"Score" ,Value: { "$gt" : "-27" }} } })
2. db.getCollection('Resource').find({ Attributes: { $elemMatch: { Key:"Score" ,Value: { "$gt" : -27 }} } })

MongoDB指南针:

{ "Attributes": { $elemMatch: { Key: { $eq: "Score" } ,Value: { $gt:"-27"}  } } }

我在这里缺少什么?

标签: mongodbmongodb-queryrobo3t

解决方案


推荐阅读