Everything You Need to Know About Mongoose
ODM(Object Data Modeling) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
A tool which provide data modeling and help connecting to mongoDB
index.js
const mongoose = require('mongoose');
app.get('/mongodb', async (req, res) => {
await mongoose.connect('mongodb://localhost/voyage', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
useCreateIndex: true
});
res.send('ok');
})
to show one product
index.js
const mongoose = require('mongoose');
app.get('/mongodb', async (req, res) => {
await mongoose.connect('mongodb://localhost/voyage', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
useCreateIndex: true
}); // options
const { Schema } = mongoose;
const goodsSchema = new Schema({
goodsId: {type: Number, required: true, unique: true},
name: {type: String, required: true, unique: true},
thumbnailUrl: {type: String},
category: {type: String},
price: {type: Number}
});
let Goods = mongoose.model("Goods", goodsSchema);
await Goods.create({
goodsId: 1,
name: "dinner",
thumbnailUrl: "<https://purewows3.imgix.net/images/articles/2019_04/one-pan-spaghetti-meatballs-recipe-290.jpg?auto=format,compress&cs=strip>",
category: "food",
price: 20000
});
res.send('OK');
});