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);
});
});