Skip to main content

Posts

Showing posts with the label MongoDb Sort by Date

MongoDB update if else

Syntax: db . Users . update ( criteria , update [[, options] , callback ] ); According to MongoDB , ·          A criteria is a query object to find records that need to be updated. ·          Update is the replacement object and the update has multiple options. o     multi : Update all the records that match the query object. o     upsert : If no records match then insert update as a new records. o     raw : returns updated document as json and by default  return false. ·          An option is an options object. ·          Callback is the callback to be run after the records is updated. Example: The matching documents will be replaced                    db . Users . update ({ _id: "1001" }, { na...

MongoDb aggregation if else

Syntax: { $cond : [<expression>, <true>, <false>] } Example as given below db . Customers . aggregate ( [     { "$project": {         "name": 1,         "customer": {             "$cond": {                 "if": { "$eq": [ "$Age", 25 ] },                 "then" : "YG" ,                 "else" : {                     "$cond" : {                         "if" : { "$eq" ...

How to sort a collection by date in MongoDb?

In MongoDb , different ways of sorting can perform on a collection and you can use 1 for ‘asc’|ascending and -1 for ‘desc’|descending. Syntax: db . collection . find (). sort ( { column1: 1 or - 1 [, column2: 1 or -1] }); For example, db . customers . find (). sort ({ CreatedDate: 'desc' }). toArray ( function ( error ,   items ) {});                                                             OR db . customers . find (). sort ( 'CreatedDate' , 'desc' ). toArray ( function ( error ,   items ) {});                            ...