Mongoose : insertMany() function / insert multiple documents....[Schema/Model]
swapnanilWebDeveloper
Posted on February 6, 2024
1)Here you guys are going to learn about Schema , models and how the model of a particular schema can create a reference to database for collection where multiple documents will be inserted.
2)Let's create a TankInformation data base and then create a schema called TankSchema and then then we will create a model called Tank .
3)if the model is "Tank" then collection name regarding to this will be "tanks" ...
4)Then multiple documents will be inserted into this Tank model.
link to to this code is on my github profile
const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/TankInformation');
// creating Schema
const TankSchema = new mongoose.Schema({
name: String,
size : String,
EnteredService : Number,
CountryOrigin : String,
NumberBuilt : {type : Number, min : 10, max : 25000},
MassTonns : {type : Number, min : 5, max : 100},
EnginePowerHP : Number,
RangeKM : Number,
});
// creating model
const Tank = mongoose.model('Tank', TankSchema);
await Tank.insertMany([
{
name: 'M60',
size : "medium",
EnteredService : 1959,
CountryOrigin : "United States of America",
NumberBuilt : 15000,
MassTonns : 46,
EnginePowerHP : 750,
RangeKM : 450,
},
{
name: 'T_62',
size : "medium-large",
EnteredService : 1961,
CountryOrigin : "Soviet Union",
NumberBuilt : 22700,
MassTonns : 37,
EnginePowerHP : 620,
RangeKM : 320,
},
{
name: 'Leopard_1',
size : "large",
EnteredService : 1999,
CountryOrigin : "Canada",
NumberBuilt : 114,
MassTonns : 43,
EnginePowerHP : 830,
RangeKM : 600,
},
{
name: 'Panzer_61',
size : "small-medium",
EnteredService : 1965,
CountryOrigin : "Switzerland",
NumberBuilt : 150,
MassTonns : 39,
EnginePowerHP : 630,
RangeKM : 250,
}
]);
const all_TankData = await Tank.find();
console.log(all_TankData);
for(var i = 0; i < all_TankData.length; i++){
console.log("name is = "+all_TankData[i].name+", size is = "+all_TankData[i].size+", year when it entered service = "+all_TankData[i].EnteredService+
"\n country from where originated = "+all_TankData[i].CountryOrigin+", number of tanks built = "+all_TankData[i].NumberBuilt+
"\n mass is = "+all_TankData[i].MassTonns+" Tonns"+", Engine Power is = "+all_TankData[i].EnginePowerHP+" HP "+", range is = "+all_TankData[i].RangeKM+" Km\n");
}
output :
[
{
_id: new ObjectId('65c2abe703b6e70019622ca3'),
name: 'M60',
size: 'medium',
EnteredService: 1959,
CountryOrigin: 'United States of America',
NumberBuilt: 15000,
MassTonns: 46,
EnginePowerHP: 750,
RangeKM: 450,
__v: 0
},
{
_id: new ObjectId('65c2abe703b6e70019622ca4'),
name: 'T_62',
size: 'medium-large',
EnteredService: 1961,
CountryOrigin: 'Soviet Union',
NumberBuilt: 22700,
MassTonns: 37,
EnginePowerHP: 620,
RangeKM: 320,
__v: 0
},
{
_id: new ObjectId('65c2abe703b6e70019622ca5'),
name: 'Leopard_1',
size: 'large',
EnteredService: 1999,
CountryOrigin: 'Canada',
NumberBuilt: 114,
MassTonns: 43,
EnginePowerHP: 830,
RangeKM: 600,
__v: 0
},
{
_id: new ObjectId('65c2abe703b6e70019622ca6'),
name: 'Panzer_61',
size: 'small-medium',
EnteredService: 1965,
CountryOrigin: 'Switzerland',
NumberBuilt: 150,
MassTonns: 39,
EnginePowerHP: 630,
RangeKM: 250,
__v: 0
}
]
name is = M60, size is = medium, year when it entered service = 1959
country from where originated = United States of America, number of tanks built = 15000
mass is = 46 Tonns, Engine Power is = 750 HP , range is = 450 Km
name is = T_62, size is = medium-large, year when it entered service = 1961
country from where originated = Soviet Union, number of tanks built = 22700
mass is = 37 Tonns, Engine Power is = 620 HP , range is = 320 Km
name is = Leopard_1, size is = large, year when it entered service = 1999
country from where originated = Canada, number of tanks built = 114
mass is = 43 Tonns, Engine Power is = 830 HP , range is = 600 Km
name is = Panzer_61, size is = small-medium, year when it entered service = 1965
country from where originated = Switzerland, number of tanks built = 150
mass is = 39 Tonns, Engine Power is = 630 HP , range is = 250 Km
đ đȘ đ
đ©
swapnanilWebDeveloper
Posted on February 6, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript Building a Self-Evolving Codebase: JavaScriptâs AST Manipulation and Dynamic Compilation
November 5, 2024
javascript Mongoose : insertMany() function / insert multiple documents....[Schema/Model]
February 6, 2024