选择两个不同字段之间的范围包含给定数字的数据 [英] Select data where the range between two different fields contains a given number
问题描述
我想在数据库中查询输入值介于或等于这两个字段LOC_CEP_INI
和LOC_CEP_FIM
I want to make a find query on my database for documents that have an input value between or equal to these 2 fields, LOC_CEP_INI
and LOC_CEP_FIM
示例:用户向系统输入了一个值:69923994,然后使用此输入在数据库中搜索所有具有该值的字段LOC_CEP_INI
和LOC_CEP_FIM
之间的文件.
Example: user input a number to the system with value : 69923994, then I use this input to search my database for all documents that have this value between the range of the fields LOC_CEP_INI
and LOC_CEP_FIM
.
我的一个文档(在此示例中,由于输入在范围内,因此查询选择了此文档):
One of my documents (in this example this document is selected by the query because the input is inside the range):
{
"_id" : ObjectId("570d57de457405a61b183ac6"),
"LOC_CEP_FIM" : 69923999, //this field is number
"LOC_CEP_INI" : 69900001, // this field is number
"LOC_NO" : "RIO BRANCO",
"LOC_NU" : "00000016",
"MUN_NU" : "1200401",
"UFE_SG" : "AC",
"create_date" : ISODate("2016-04-12T20:17:34.397Z"),
"__v" : 0
}
推荐答案
db.collection.find( { field: { $gt: value1, $lt: value2 } } );
https://docs.mongodb.com/v3.2/reference/method/db.collection.find/ 使用$ gt和$ lt引用此mongo提供范围工具.
https://docs.mongodb.com/v3.2/reference/method/db.collection.find/ refer this mongo provide range facility with $gt and $lt .
这篇关于选择两个不同字段之间的范围包含给定数字的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!