安裝 Mongoose
npm install mongoose --save
導(dǎo)入中間件
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
鏈接數(shù)據(jù)庫
var db = mongoose.createConnection('localhost', 'lyTest');
定義 Schema, 也就是傳統(tǒng)意義的表結(jié)構(gòu)
var personSchema = new mongoose.Schema({
_name: String, //定義一個(gè)屬性 name, 類型為 String
_phone: Number
});
定義 Model
var personModel = db.model('luoyanArr', personSchema);
常用操作 CRUD
測試數(shù)據(jù)
var obj = {
_name: 'luoyan',
_phone: 111
}
添加數(shù)據(jù)
// 方法 1
var personEntity = new personModel(obj);
personEntity.save(function (err) {
if (err) {
console.log('insert failed');
} else {
console.log('insert success');
}
});
// 方法 2
personModel.create(obj, function (err) {
if (err) {
console.log('insert failed');
} else {
console.log('insert success');
}
});
注意: 在添加數(shù)據(jù)的時(shí)候, 如果不指定 id 屬性, id 會默認(rèn)為一個(gè)字符串對象.
區(qū)別: Entity 是 model 的對象, 用它來添加數(shù)據(jù)的時(shí)候會把隱藏屬性一起存入數(shù)據(jù)庫, 有幾率報(bào)錯(cuò) model 只能添加純凈的 json 對象, 不能添加它創(chuàng)建的實(shí)體.
修改操作
// 方法 1
personModel.update({_phone: 222}, {$set: {_name: '把222的都改了'}}, { multi: true}, function(err){
if (err) { // 錯(cuò)誤
console.log('update failed');
} else { // 正確
console.log('update success');
}
});
// 方法 2
var id = "56fc1bbe7c2bce52c38fc5f8";
personModel.findById(id, function(err, person){
if (err) {
console.log('update failed');
} else {
person._name = 'from_id';
person.save(function(err){ ... });
console.log('update success');
}
});
注意: 如果更新的數(shù)據(jù)比較少的話, 用方法一很方便的, 其中的 { multi: true } 表示同時(shí)更新所有數(shù)據(jù), { multi: false } 或者省略則只跟新一條
查詢數(shù)據(jù)
直接查詢
personModel.findOne({'_phone': 111}, function (err, person) {
if (err) {
console.log('select failed');
} else {
console.log( person );
}
});
查詢一條
var query = personModel.findOne({'_phone': 111});
query.select();
query.exec(function(err, person){
if (err) {
console.log('select failed');
} else {
console.log(person);
}
});
查詢多條
var query = personModel.find({'_phone': 222});
query.select();
query.exec(function(err, person){
if (err) {
console.log('select failed');
} else {
console.log(person);
}
});
鏈?zhǔn)讲樵?/h3>
personModel
.where('_phone')
.where('_name').equals('aaa')
.limit(2)
.select()
.exec(function (err, person) {
if (err) {
console.log('select failed');
} else {
console.log(person);
}
});
另外附詳細(xì)鏈?zhǔn)讲樵儽?/h3>
personModel //model 對象
.find({ occupation: /host/ }) // 條件
.where('name.last').equals('Ghost') //字段 = 值
.where('age').gt(17).lt(66) // 范圍
.where('likes').in(['vaporizing', 'talking']) // 范圍
.limit(10) // 查幾條
.skip(20) // 跳過幾條
.asc('age') // 排序
.sort('-occupation')
.slaveOk()
.hint({ age: 1, name: 1 })
.select('name occupation') // 執(zhí)行查詢 () 中的值可以省略, 只是別名
.exec(callback); // 回調(diào)函數(shù), 處理查詢結(jié)果
刪除操作
var personEntity = new personModel();
personModel.remove({_id:"56fc281ab64732d8c33b4db7"}, function (err) {
if (err) {
console.log('delete failed');
} else {
console.log('detete success');
}
});
注意: 刪除條件一定是唯一的, 不然就清庫了.
personModel
.where('_phone')
.where('_name').equals('aaa')
.limit(2)
.select()
.exec(function (err, person) {
if (err) {
console.log('select failed');
} else {
console.log(person);
}
});
personModel //model 對象
.find({ occupation: /host/ }) // 條件
.where('name.last').equals('Ghost') //字段 = 值
.where('age').gt(17).lt(66) // 范圍
.where('likes').in(['vaporizing', 'talking']) // 范圍
.limit(10) // 查幾條
.skip(20) // 跳過幾條
.asc('age') // 排序
.sort('-occupation')
.slaveOk()
.hint({ age: 1, name: 1 })
.select('name occupation') // 執(zhí)行查詢 () 中的值可以省略, 只是別名
.exec(callback); // 回調(diào)函數(shù), 處理查詢結(jié)果
刪除操作
var personEntity = new personModel();
personModel.remove({_id:"56fc281ab64732d8c33b4db7"}, function (err) {
if (err) {
console.log('delete failed');
} else {
console.log('detete success');
}
});
注意: 刪除條件一定是唯一的, 不然就清庫了.