Data in MongoDB is made up of three types of components: databases, collections, and documents. The database sits at the top of the hierarchy, collections at the next level, and documents at the bottom.
//open the connection mongoose.connect("mongodb+srv://dxexperiments-user:DrEuMg7KfFImh774@dxvignettes.vtnenqy.mongodb.net/");
// this lays out the foundation for every new fruit document that will be added to the database const fruitSchema = new mongoose.Schema({ name: String, rating:{ type:Number, min: 1, max: 10 }, review: String });
// first parameter, name of the collection(mongodb will convert this string into a pluralize form to create your collection); so we created a new collection called Fruits // second parameter: It is the schema of the collection. const Fruit = mongoose.model("Fruit", fruitSchema);
// creating fruit document from Fruit model, means the document need to stick to this schema const fruit = new Fruit({ name:"apple", rating: 6, review:"well, average." });
//save fruit document into the Fruist collection inside of the DATABASE // fruit.save();
const personSchema = new mongoose.Schema({ name: String, age:Number });
const grape = new Fruit({ name: "grape", rating: 7, review: " I really like it" });
const kiwi = new Fruit({ name: "kiwi", rating: 6, review: " I really like it" });
const pineapple = new Fruit({ name: "pineapple", rating: 9, review: " I really like it" }); // Fruit.insertMany([kiwi,grape,pineapple]);