luwak
$ npm install mongoose --save
Cute Crab
$ npm install mongoose --save
npm i mongoose // install npm package first
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
// create user
const User = mongoose.model('User', { name: String });
const kitty = new User({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
const mongoose = require("mongoose");
// use .env to connect
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log(`mongodb connected :${conn.connection.host}`);
} catch (error) {
console.error(`Error:${error.message}`);
process.exit();
}
};
module.exports = connectDB;
---------Create Model or Collection-----------
import { Schema, model } from "mongoose"
let userSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true, },
role: { type: "String", default: "customer" }
})
export const userModel = model("User", userSchema, "users")
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
const Cat = mongoose.model('Cat', { name: String });
const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
// schema creation...
let modal;
SchemaVariable.pre('save', function(next) {
if (this.isNew) {
if (!modal) {
modal = mongoose.model('collectionname');
}
modal.find({})
.then((entries) => {
this._id = entries.length + 1;
next();
})
}
});
mongoose
.connect("mongodb://localhost:27017/blogDB", {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("Database connected!"))
.catch(err => console.log(err));
const adirelad = "a";
no, he doesn't say among us