一、简介
注:无意间看到的一张表,便于理解
分析比较到位的一篇文章【转载】深入浅出mongoose
- Schema
- Model
二、安装
npm install --save mongoose
导入
// Using Node.js `require()`
const mongoose = require('mongoose');
// Using ES6 imports
import mongoose from 'mongoose';
IDE 可以安装Mongo plugin
三、使用
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test', {useNewUrlParser:true})
var db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', () => console.log('Mongodb connected.'))
var userSchema = new mongoose.Schema({
name:String,
age:Number,
sex:{type:Number, default:0}
})
var User = mongoose.model('User', userSchema)
User.create({
name:'莫邪'
}, (err) => {
if (err)
console.log(err)
})
User.create({
name:'左丘',
age:12
}, (err, small)=>console.log(small))
// User.deleteMany({}) //remove function deprecated,Use deleteOne, deleteMany, or bulkWrite instead.