Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongodb working with date

// sample data
{
  "_id" : 1,
  "item" : "abc",
  "price" : 10,
  "quantity" : 2,
  "date" : ISODate("2014-01-01T08:15:39.736Z")
}

//  query
db.sales.aggregate(
   [
     {
       $project:
         {
           year: { $year: "$date" },
           month: { $month: "$date" },
           day: { $dayOfMonth: "$date" },
           hour: { $hour: "$date" },
           minutes: { $minute: "$date" },
           seconds: { $second: "$date" },
           milliseconds: { $millisecond: "$date" },
           dayOfYear: { $dayOfYear: "$date" },
           dayOfWeek: { $dayOfWeek: "$date" },
           week: { $week: "$date" }
         }
     }
   ]
)
// result:
{
  "_id" : 1,
  "year" : 2014,
  "month" : 1,
  "day" : 1,
  "hour" : 8,
  "minutes" : 15,
  "seconds" : 39,
  "milliseconds" : 736,
  "dayOfYear" : 1,
  "dayOfWeek" : 4,
  "week" : 0
}



Comment

PREVIOUS NEXT
Code Example
Javascript :: type coercion 
Javascript :: plotly express bar graph 
Javascript :: expo modal 
Javascript :: pop array 
Javascript :: map a square to a circle 
Javascript :: office check in 
Javascript :: url encoded body in node.js 
Javascript :: HOW TO ADD INDEXES TO EXISTING COLLECTIONS mongodb 
Javascript :: npm set author name 
Javascript :: npm ERR! Error: connect ECONNREFUSED 
Javascript :: convert days in years js 
Javascript :: combine the values of 2 arrays in key = value jquery 
Javascript :: %PDF-1.4 is response 
Javascript :: for loop in javascript 
Javascript :: javascript comments 
Javascript :: datatable bootstrap cllick on specific button 
Javascript :: javascript turn negative number to positibve 
Javascript :: days between two dates 
Javascript :: convert datetime to timestamp javascript 
Javascript :: jquery select selected get data name 
Javascript :: react currency format method 
Javascript :: try catch with for loop in javascript 
Javascript :: delete from array javascript 
Javascript :: how to get location javascript 
Javascript :: javascript for loop[ 
Javascript :: Send Email sgMail 
Javascript :: conditional onclick react 
Javascript :: js difference between two arrays of objects 
Javascript :: javascript Spread Operator with Object 
Javascript :: on button click show collapse div jquery 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =