Mongoose timestamps are supported by the schema. Timestamps save the current time of the document created and also when it was updated in form of a Date by turning it true. When set to true, the mongoose creates two fields as follows:
- createdAt: Date representing when the document was created
- updatedAt: Date representing when this document was last updated
The two fields are created when the database was first created and then updated by the queries save(), updateOne(), updateMany(), findOneAndUpdate(), update(), replaceOne(), or bulkWrite().
Syntax: Create schema with a timestamp as follows:
const studentSchema = new Schema({ name: String }, { timestamps: true });
const Student = mongoose.model(‘Student’, studentSchema);
Creating Application and installing Modules: We will create a schema with the timestamps and then print the different timestamps that are createdAt and updatedAt by updating the details of a student.
Step 1: Create a folder and initialize it:
npm initStep 2: Install mongoose in the project.
npm i mongooseProject Structure: The project structure is as follows:
Example: Create a file called index.js. Inside the index.js, connect to MongoDB. Here the MongoDB Compass is used. Now first create the Student schema and then its model. Now create a new document and save it. Print the document timestamps and then update the document after a time delay and then again print the timestamp details.
index.js
const mongoose = require("mongoose");
// Database connection
mongoose.connect("mongodb://localhost:27017/geeksforgeeks");
npm i mongoose2
npm i mongoose3npm i mongoose4 npm i mongoose5
npm i mongoose6npm i mongoose7npm i mongoose8 npm i mongoose9
npm i mongoose6node index.js1node index.js2node index.js3
node index.js4npm i mongoose9
node index.js4node index.js7npm i mongoose8 node index.js9
);
const mongoose = require(2
const mongoose = require(3const mongoose = require(4const mongoose = require(5
const mongoose = require(7
const mongoose = require(9
"mongoose"0
node index.js4"mongoose"2npm i mongoose4 "mongoose"4
npm i mongoose6"mongoose"6
npm i mongoose6"mongoose"8
node index.js4);0
node index.js4);2
npm i mongoose6);4);5);6);7);8
npm i mongoose6);4 1 2
npm i mongoose6);4 5 6
node index.js4);0
9
// Database connection1
node index.js4// Database connection3
npm i mongoose6// Database connection5// Database connection6 npm i mongoose9
npm i mongoose6// Database connection9
npm i mongoose6mongoose.connect(1npm i mongoose4mongoose.connect(3npm i mongoose8 node index.js9
node index.js4);
node index.js4);4);5);6);7);8
node index.js4);4 1 2
node index.js4);4 5 6
9
npm i mongoose04
node index.js4npm i mongoose06// Database connection6npm i mongoose08
node index.js4npm i mongoose10npm i mongoose11 npm i mongoose12
npm i mongoose6npm i mongoose14
node index.js4npm i mongoose16
9
npm i mongoose19
Step 4: Run the code using the following command:
node index.jsOutput: The output in the command line is as follows. The output shows that the document is updated after 3 seconds.
Output of MongoDB: The following field will now reflect in the database also, as shown in the image below: