Sunday 11 August 2013

MongoDB basic commands examples

Lets say that you have number of records in your MongoDB:

{
    "employee_id": 18,
    "type": "permanent",
    "salary": 25.943819004429677
}
{
    "employee_id": 183,
    "type": "permanent",
    "salary": 30.390389004427579
}
{
    "employee_id": 95,
    "type": "permanent",
    "salary": 45.941144137631879
}
{
    "employee_id": 158,
    "type": "temp",
    "salary": 27.645113212427579
}

You can search for all permanent job types with salary greater than or equal to 30, display employee_id, type and salary and sort those salaries from lowest to highest (ascending order).

db.collection.find({"type": "permanent", salary: {"$gte" : 30}},{"employee_id": true, "type": true, "salary": true}).sort({"salary": 1}).pretty()

No comments:

Post a Comment