Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mongodb - Select data where the range between two different fields contains a given number

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

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
db.collection.find( { field: { $gt: value1, $lt: value2 } } );

https://docs.mongodb.com/v3.2/reference/method/db.collection.find/ refer this mongo provide range facility with $gt and $lt .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...