mogoose使用

本文介绍了使用 Mongoose 操作 MongoDB 的过程,包括定义数据模型、进行数据增删改查等基本操作。通过示例代码展示了如何连接数据库、定义 Schema 和 Model,以及执行具体的 CRUD 操作。
摘要由CSDN通过智能技术生成

mongoose
–schema
base.js
–model
base.js
start.js

schema/base.js

/**
 * Created by jiawei.tu on 8/17 0017.
 */

var mongoose = require('mongoose');

var userSchema = mongoose.Schema({
    _id: Number,
    username: String,
    password: String,
    salt: String,
    email: String,
    profile: String
});

var postSchema = mongoose.Schema({
    _id: Number,
    title: String,
    content: String,
    tags: [Number],
    status: Number,
    createTime: Date,
    updateTime: Date,
    authorId: Number
});

var tagSchema = mongoose.Schema({
    _id: Number,
    name: String,
    frequency: Number
});

module.exports.userSchema = userSchema;
module.exports.postSchema = postSchema;
module.exports.tagSchema = tagSchema;

model/base.js

/**
 * Created by jiawei.tu on 8/17 0017.
 */

var mongoose = require('mongoose');
var baseSchema = require('../schema/base');

var userModel = mongoose.model('userModel', baseSchema.userSchema);
var postModel = mongoose.model('postModel', baseSchema.postSchema);
var tagModel = mongoose.model('tagModel', baseSchema.tagSchema);

module.exports.userModel = userModel;
module.exports.postModel = postModel;
module.exports.tagModel = tagModel;

start.js

var async = require('async');
var mongoose = require('mongoose');
var baseModel = require('./model/base');

mongoose.connect('mongodb://localhost/myblog');
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error;'));

var addData = function(callback) {
    // 增加
    var ids = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    async.each(ids, function(id, callback) {
        var user = new baseModel.userModel({
            _id: id,
            username: "tujiaw" + id,
            password: "tujiaw" + id,
            salt: "",
            email: "tujiaw@live.com",
            profile: "profile profile profile profile"
        });
        user.save(function(err, data) {
        });
        callback(null);
    }, function(err) {
        callback(err);
    });
};

var queryData = function(callback) {
    // 查找
    async.parallel({
        one: function(callback) {
            baseModel.userModel.find(function(err, users) {
                callback(err, users);
            });
        },
        two: function(callback) {
            baseModel.userModel.find({_id: 2}, function(err, users) {
                callback(err, users);
            });
        },
        three: function(callback) {
            baseModel.userModel.findOne({_id: 3}, function(err, user) {
                callback(err, user);
            });
        },
        four: function(callback) {
            baseModel.userModel.findById(4, function(err, user) {
                callback(err, user);
            });
        }
    }, function(err, results) {
        callback(err);
        console.log("---queryData:" + results.two);
    });
};

var removeData = function(callback) {
    // 删除
    async.parallel({
        one: function(callback) {
            baseModel.userModel.remove({username: "tujiaw5"}, function(err, user) {
                callback(err, user);
            });
        },
        two: function(callback) {
            baseModel.userModel.findByIdAndRemove(6, function(err, user) {
                callback(err, user);
            });
        },
        three: function(callback) {
            baseModel.userModel.findOneAndRemove({_id: 7}, function(err, user) {
                callback(err, user);
            });
        }
    }, function(err, results) {
        callback(err);
        console.log("---removeData:" + results.one);
    });
};

var updateData = function(callback) {
    // 更新
    async.parallel({
        one: function(callback) {
            baseModel.userModel.findOneAndUpdate({_id: 8}, {username: "tujiaw8888"}, function(err, user) {
                callback(err, user);
            });
        }
    }, function(err, results) {
        callback(err);
        console.log("---updateData" );
    });
};

db.once('open', function(callback) {
    console.log('open success');
    async.series({
        one: function(callback) {
            addData(callback);
        },
        two: function(callback) {
            queryData(callback);
        },
        three: function(callback) {
            removeData(callback);
        },
        four: function(callback) {
            updateData(callback);
        }
    }, function(err, results) {
        if (err) {
            console.log(err);
        }
        console.log(results);
    });
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值