Home:ALL Converter>how to query array with specific condition in MongoDB

how to query array with specific condition in MongoDB

Ask Time:2014-06-25T16:42:02         Author:Eugene Yu

Json Formatter

here's my mongodb object example

{
    "_id": ObjectId("asdklfjasdlkfjal"),
    "geometry": {
        "type": "Point",
        "coordinates": [
            -26.62375,
            152.86114
        ]
    }
},
{
    "_id": ObjectId("asdklfjasdlkfjal2"),
    "geometry": {
        "type": "Point",
        "coordinates": [
            -28.62375,
            123.86114
        ]
    }
}

I have read the document here but it does not show me an option to query only the first element of the array.

I've tried the following line on MongoHub but it gives me "invalid operator: $and" msg

{"geometry.coordinates": {$and: [{$lt: -30, $gt: 151}, {$lt: -35, $gt: 151}]}}

For example, I'd like to query the elements that have the value greater than -27 as the first value of the array. So only the first example object should be pulled no matter what value the second element has in the array (or the other way around).

Also found the same question here but it was 3yrs ago so thought there should be a better way by now.

Thanks for reading my question.

Author:Eugene Yu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24403974/how-to-query-array-with-specific-condition-in-mongodb
yy