Reputation: 97
Noob question but how do I create multiple documents of the same schema without having to declare a new const. Do I need to create an array for each key e.g
const newItem = new Item({
name: ["Blue Shirt", "Green Shirt"],
image: ["images/image-1.jpg","images/image-2.jpg"],
price: [30, 20],
quantity: [5, 10]
});
<------------Orginal Code ------------->
const newItem = new Item({
name: "Blue Shirt",
image: "https://picsum.photos/id/237/200/300",
price: 30,
quantity: 5
});
Upvotes: 1
Views: 417
Reputation: 1499
You have to create an array of your data.
const newItem = new Item({
name: "Blue Shirt",
image: "https://picsum.photos/id/237/200/300",
price: 30,
quantity: 5
});
var arr = [newItem]; //Pass array of documents / objects.
Item.insertMany(arr, function(error, docs) {});
Or
var arr = ['{
name: "Blue Shirt",
image: "https://picsum.photos/id/237/200/300",
price: 30,
quantity: 5
}', {
name: "Blue Shirt",
image: "https://picsum.photos/id/237/200/300",
price: 30,
quantity: 5
}]; //Pass array of documents / objects.
Item.insertMany(arr, function(error, docs) {});
You can explore more on https://mongoosejs.com/docs/api.html#model_Model.insertMany
Upvotes: 1