3

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
}

4 Answers 4

11
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 .

Sign up to request clarification or add additional context in comments.

Comments

3

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.

5 Comments

Humm and if i dont have a array property?
then you just write the code example i gave. What i meant is that if you write db.zipcodes.find({array: ...}), mongo will try to match your condition on an array prop of your documents. If they don't have one, just skip it, and write the $elemMatch condition directly
#VonD i tried like you said with your code but i receive a error Error: error: { "waitedMS" : NumberLong(0), "ok" : 0, "errmsg" : "unknown top level operator: $elemMatch", "code" : 2 }
tried with db.zipcodes.find({ $elemMatch: { LOC_CEP_INI: {$gte: 69923997}, LOC_CEP_FIM: {$lte: 69923997} } });
@michelpm1 sorry, i got confused with this array thing. I've edited my answer. $elemMatch is useful if one of your props is an array. Let's say your documents as an items props and that each item has the LOC_CEP_INI and LOC_CEP_FIM props, then you would write {items: {$elemMatch: { LOC_CEP_INI: {$gte: 69923997}, LOC_CEP_FIM: {$lte: 69923997} }}}
0

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

Comments

0

Here is the logic use it as per the need:

 Userdb.aggregate([
    { "$match": { _id: ObjectId(session._id)}},
    { $project: {
        checkout_list: {
            $filter: {
                input: "$checkout_list",
                as: "checkout_list",
                cond: {
                    $and: [
                        { $gte: [ "$$checkout_list.createdAt", new Date(date1) ] },
                        { $lt: [ "$$checkout_list.createdAt", new Date(date2) ] }
                    ]
                }    
            }
        }
    }
}

Here i use filter, because of some reason data query on nested data is not gets succeed in mongodb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.