MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Cercare date e ignorare l'ora in mongoDB

Un modo per farlo sarebbe usare l'aggregation framework e sfrutta gli date aggregation operators . Ad esempio:

db.camps.aggregate(
   [
     // Perform the initial match to filter docs by 'status' and 'camp_id'
     {
       $match:
         {
           status: 1,
           camp_id: pCampID
         }
     },
     // Extract the year, month and day portions of the 'enddate' 
     {
       $project:
         {
           year: { $year: "$enddate" },
           month: { $month: "$enddate" },
           day: { $dayOfMonth: "$enddate" },
           status: 1,
           camp_id: 1
         }
     },
     // Filter by date - Replace hard-coded values with values you want
     {
       $match:
         {
           year: { $gte: 2014 },
           month: { $gte: 10 },
           day: { $gte: 10 }
         }
     }
   ]
)