Sajeetharan
Sajeetharan

Reputation: 222582

How to pass a query to mongoose and get specific data?

I am new to Node and MongoDb, i have an object like this in the database,

  [{
    "_id": "5890b47a6166c457ffdee2bb",
    "description": "Capital One Beach Bash: Free Orange Bowl concert on South Beach",
    "name": "Imagine Dragons: Orange Bowl",
    "place": {
        "name": "Lummus Park, Miami Beach",
        "location": {
            "city": "Miami",
            "country": "United States",
            "latitude": 25.7804,
            "longitude": -80.1299,
            "state": "FL",
            "zip": "33139"
        },
        "id": "166795223482547",
        "_id": "5890b4956166c457ffdee46c"
    },
    "start_time": "2015-12-30T19:00:00-0500",
    "id": "730714023727940"
}]

I have created a schema for this above json with mongoose, i can retrieve the entire dataset using the following,

model.find(conditon, {}, options, function(error, data) {
}

How can i retrieve the top 10 events for the country "United States" ? how to write a custom query inside the condition?

Upvotes: 2

Views: 576

Answers (1)

Jyotman Singh
Jyotman Singh

Reputation: 11340

To query nested objects you can use the dot(.) notation.

To get limited results you can use the limit option.

So your query will be like this -

model.find({'place.location.country': 'United States'}, {}, {limit: 10}, function(error, data) {});

Upvotes: 3

Related Questions