mongoose 演示增删改查,完整版案例

mongoose 演示增删改查,完整版案例


说明:dadi数据库下有users 和news表,详细演示增删改查。

一、处理dadi/users

find

// 1、引入mongoose
const mongoose = require('mongoose');

// 2、建立连接
mongoose.connect('mongodb://localhost/dadi', {
    // 配置:去掉警告信息
    useUnifiedTopology: true ,
    useNewUrlParser: true
})

// 3、操作users表(集合) 
// 定义一个Schema,schema里的对象和数据库里的字段一一对应
var UserSchema = mongoose.Schema({
    name: String,
    age: Number,
    status: Number,
})

// 4、定义数据库模型 操作数据库 
// model 里面的第一个参数要注意:1.首字母大写 2.要和数据库(集合)名称对应 这个模型和会模型名称相同的附属的数据库表建立连接

var User = mongoose.model('User', UserSchema);

// 5、查询users表的数据
User.find({}, function(err, doc){
    if(err){
        console.log('err: ', err);
        return;
    }
    console.log('doc', doc);
})

create

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dadi', {
    useUnifiedTopology: true ,
    useNewUrlParser: true
})

var UserSchema = mongoose.Schema({
    name: String,
    age: Number,
    status: Number,
})

var User = mongoose.model('User', UserSchema);

/**
 * 增加数据
 * 1.实例化Modal  2.实例 .save()
*/
var u = new User({
    name: 'zhangsan3',
    age: 22,
    status: 1
})

u.save(function(err) {
    if(err){
        console.log('err: ', err);
        return;
    }    
    console.log('增加数据成功');
})

二、处理dadi/news

注:之前没有news集合

create

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dadi', {
    useUnifiedTopology: true ,
    useNewUrlParser: true
})

var NewsSchema = mongoose.Schema({
    title: String,
    author: String,
    pic: String,
    status: Number
})

// 定义操作数据库的modal, 第三个参数表示那个表
var News = mongoose.model('News', NewsSchema, 'news');

/**
 * 增加数据
 * 1.实例化Modal  2.实例 .save()
*/
var news = new News({
    title: '新闻1',
    author: '玄空1',
    pic: '1.png',
    status: 1
})

news.save(function(err) {
    if(err){
        console.log('err: ', err);
        return;
    }    
    console.log('增加数据成功');
})

find

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dadi', {
    useUnifiedTopology: true ,
    useNewUrlParser: true
})

var NewsSchema = mongoose.Schema({
    title: String,
    author: String,
    pic: String,
    status: Number
})

// 定义操作数据库的modal, 第三个参数表示那个表
var News = mongoose.model('News', NewsSchema, 'news');

News.find({}, (err, doc) => {
    if(err){
        console.log('err: ', err);
        return;
    }
    console.log('查找成功', doc);
})

update

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dadi', {
    useUnifiedTopology: true ,
    useNewUrlParser: true
})
var NewsSchema = mongoose.Schema({
    title: String,
    author: String,
    pic: String,
    status: Number
})
var News = mongoose.model('News', NewsSchema, 'news');

News.updateOne(
    {'_id': '601bdaf50907205e112a9d8d'},
    {title: '新闻1-update'},
    (err, doc) => {
        if(err){
            console.log('err: ', err);
            return;
        }
        console.log('查找成功', doc);
    }
)

delete

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dadi', {
    useUnifiedTopology: true ,
    useNewUrlParser: true
})
var NewsSchema = mongoose.Schema({
    title: String,
    author: String,
    pic: String,
    status: Number
})
var News = mongoose.model('News', NewsSchema, 'news');

News.deleteOne(
    {'_id': '601bdaf50907205e112a9d8d'},
    (err, doc) => {
        if(err){
            console.log('err: ', err);
            return;
        }
        console.log('查找成功', doc);
    }    
)
上一篇:RESTful API


下一篇:JMeter怎么在get URL请求、POST请求中添加动态参数用于服务器段安全验证