-
Notifications
You must be signed in to change notification settings - Fork 0
/
step04-create-multiple-documents.js
40 lines (35 loc) · 1.1 KB
/
step04-create-multiple-documents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var mongoose = require('mongoose');
var dotenv = require('dotenv');
dotenv.config();
(async ()=>{
try {
await mongoose.connect(process.env.MONGODB_CONNECTION_STRING,{ useNewUrlParser: true, useUnifiedTopology:true });
console.log('mongoose open for business');
//Define a schema
const studentSchema = new mongoose.Schema({
name: {type: String, index: true},
age: Number
});
//Creating a model
const Student = mongoose.model('Student', studentSchema);
// Create an instance of model 'Student'
const student1 = new Student({
name: "Inam",
age: 36
});
const student2 = new Student({
name: "Rehan",
age: 15
});
const student3 = new Student({
name: "Taha",
age: 25
});
// Save the new model instances with insertMany function and provide instance objects into function as array argument
const result = await Student.insertMany([student1,student2,student3])
console.log("Result = ", result);
}
catch(error) {
console.log(error);
}
})();