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

后端 未结 3 786
旧巷少年郎
旧巷少年郎 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 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.

提交回复
热议问题