Cara menggunakan mongodb created at timestamp

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 init

Step 2: Install mongoose in the project.

npm i mongoose

Project Structure: The project structure is as follows:

Cara menggunakan mongodb created at timestamp

 

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 mongoose
2

npm i mongoose
3
npm i mongoose
4
npm i mongoose
5

npm i mongoose
6
npm i mongoose
7
npm i mongoose
8
npm i mongoose
9

npm i mongoose
6
node index.js
1
node index.js
2
node index.js
3

node index.js
4
npm i mongoose
9

node index.js
4
node index.js
7
npm i mongoose
8
node index.js
9

);

  

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.js
4"mongoose"2
npm i mongoose
4 "mongoose"4

npm i mongoose
6"mongoose"6

npm i mongoose
6"mongoose"8

node index.js
4);0

node index.js
4);2

npm i mongoose
6);4);5);6);7);8

npm i mongoose
6);4 1 2

npm i mongoose
6);4 5 6

node index.js
4);0

 9

  

// Database connection1

node index.js
4// Database connection3

npm i mongoose
6// Database connection5// Database connection6
npm i mongoose
9

npm i mongoose
6// Database connection9

npm i mongoose
6mongoose.connect(1
npm i mongoose
4mongoose.connect(3
npm i mongoose
8
node index.js
9

node index.js
4);

node index.js
4);4);5);6);7);8

node index.js
4);4 1 2

node index.js
4);4 5 6

 9

  

npm i mongoose
04

node index.js
4
npm i mongoose
06// Database connection6
npm i mongoose
08

node index.js
4
npm i mongoose
10
npm i mongoose
11
npm i mongoose
12

npm i mongoose
6
npm i mongoose
14

node index.js
4
npm i mongoose
16

 9

  

npm i mongoose
19

Step 4: Run the code using the following command:

node index.js

Output: The output in the command line is as follows. The output shows that the document is updated after 3 seconds.

Cara menggunakan mongodb created at timestamp

 

Output of MongoDB: The following field will now reflect in the database also, as shown in the image below: