Tuesday, June 30, 2015

Helper Functions in MongoDB

> db.orders.find().count()
2
>
Sort({Fieldname:sort order})
> db.orders.find().sort({_id:-1}).pretty()
{
        "_id" : ObjectId("559125cb525a7a85c0902ffd"),
        "memos" : [
                {
                        "memo" : "on time",
                        "by" : "payment"
                },
                {
                        "memo" : "delayed",
                        "by" : "shipping"
                }
        ]
}
{
        "_id" : ObjectId("55912599525a7a85c0902ffc"),
        "memos" : [
                {
                        "memo" : "on time",
                        "by" : "shipping"
                },
                {
                        "memo" : "approved",
                        "by" : "billing"
                }
        ]
}
>


Limit(num)
> db.orders.find().limit(1).pretty()
{
        "_id" : ObjectId("55912599525a7a85c0902ffc"),
        "memos" : [
                {
                        "memo" : "on time",
                        "by" : "shipping"
                },
                {
                        "memo" : "approved",
                        "by" : "billing"
                }
        ]
}
>


No comments:

Post a Comment