简体   繁体   English

MongoDB Atlas中的数值搜索如何工作?

[英]How number value Search in MongoDB Atlas work?

I'm newbie to MongoDB Atlas.我是 MongoDB Atlas 的新手。 I want to execute a query as below,我想执行如下查询,

Sample document:样本文件:

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

Query询问

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

I tried with compound and search as below, but couldn't get what I want.我尝试使用compoundsearch如下,但无法得到我想要的。

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

This returns an error saying,这会返回一个错误说,

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

And when I make those as String it doesn't return any values.当我将它们设为 String 时,它不会返回任何值。 I understand that I'm using a text search here, but I couldn't find any other alternatives.我知道我在这里使用文本搜索,但我找不到任何其他选择。 Is it not possible to search int values in MongoDB Atlas?是否无法在 MongoDB Atlas 中搜索 int 值?

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

If you want to use $in , you can use it below... using '$in'如果你想使用$in ,你可以在下面使用它...使用 '$in'

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

Somethin if you want to try with find如果您想尝试使用 find

db.collection.find({
  $and: [
    {  message: "Hello" },
    {  status: { $nin: [1,2]}
    }
  ]
})

Working mongo playground工作mongo 游乐场

If you need to use $search , you can use array如果你需要使用$search ,你可以使用数组

$search: {
    compound:{
        must:[{
          text: {
            query: 'Hello',
            path: 'message'
          }
        }],
       
        mustNot:[
          {
          text: {
            query: [1,2],
            path: 'status'
          }
        }
    ]
  }
}
  • Try this, check if it works试试这个,看看是否有效
compound:{
    must:[{
      text: {
        query: 'Hello',
        path: 'message'
      }
    }],
   
    mustNot:[
      {
      text: {
        query: 1,
        path: 'status'
      }
    },
    {
      text: {
        query: 2,
        path: 'status'
      }
    },
    {
      regex: {
        query: "([1-2]{1})",
        path: "status",
        allowAnalyzedField: true
      }
    } 
    ]
  }

doc 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM