首页 > 解决方案 > MongoDB Atlas 中的数值搜索如何工作?

问题描述

我是 MongoDB Atlas 的新手。我想执行如下查询,

样本文件:

{
  message: "Hello",
  role: "student",
  status: 1
}

询问

search 
WHERE 
'Hello' in field 'message' 
NOT [1 OR 2] in field 'status'

我尝试了compoundsearch如下,但无法得到我想要的。

compound:{
    must:[{
      text: {
        query: 'Hello',
        path: 'message'
      }
    }],
   
    mustNot:[
      {
      text: {
        query: 1,
        path: 'status'
      }
    },
    {
      text: {
        query: 2,
        path: 'status'
      }
    }
    ]
  }

这会返回一个错误说,

"Query" must be a string (from "compund.mustNot[2].text").

当我将它们设为 String 时,它不会返回任何值。我知道我在这里使用文本搜索,但我找不到任何其他选择。不能在 MongoDB Atlas 中搜索 int 值吗?

标签: javamongodbmongodb-atlas

解决方案


db.getCollection("yourCollection").find({"message":"Hello","status":{$nin:[1,2]}});

如果你想使用$in,你可以在下面使用它...使用'$in'

db.getCollection("yourCollection").find({"message":"Hello","status":{$in:[1,2]}});

推荐阅读