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

后端 未结 3 782
旧巷少年郎
旧巷少年郎 2020-12-11 10:24

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

相关标签:
3条回答
  • 2020-12-11 10:49

    db.zipcodes.find( {
    "LOC_CEP_INI": { "$lte": 69900002 }, "LOC_CEP_FIM": { "$gte": 69900002 } })

    0 讨论(0)
  • 2020-12-11 11:05

    You have to invert your field names and query value.

    db.zipcodes.find({
        LOC_CEP_INI: {$gte: 69923997},
        LOC_CEP_FIM: {$lte: 69923997}
    });
    

    For your query example to work, you would need your documents to hold an array property, and that each item in this prop hold a 69923997 prop. Mongo would then check that this 69923997 prop has a value that is both between "LOC_CEP_INI" and "LOC_CEP_FIM" for each item in your array prop.

    Also I'm not sure whether you want LOC_CEP_INI <= 69923997 <= LOC_CEP_FIM or the contrary, so you might need to switch the $gte and $lte conditions.

    0 讨论(0)
  • 2020-12-11 11:12
    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 .

    0 讨论(0)
提交回复
热议问题