一.基本使用
1.特点:
->使用BSON存储数据
->支持相对丰富的查询操作
->支持索引
->副本集(活跃节点,备份节点)
->分片
->无模式
->部署简单方便
2.启动命令:
mongod --dbpath=G:\NodeJS\MongoDB\data\db --port=xxxxx
->dbpath:指定数据库的路径
->默认的端口号是:27017
mongo:mongo数据库的客户端工具
->mongo:默认的本机数据库
->mongo 127.0.0.1:27017 连接到指定的数据库
->show dbs:
->user dbname: e.g:use part9
->db.users.insert({"username":"Linyongzhi"}):插入数据
==>WriteResult({ "nInserted" : 1 })
->db.users.find():查看数据
==>{ "_id" : ObjectId("579eeff52a825343289a2f1f"), "username" : "Linyongzhi" }
->db.users.find({"username":"Linyongzhi"}):查看数据
==>{ "_id" : ObjectId("579eeff52a825343289a2f1f"), "username" : "Linyongzhi" }
->db.users.find().count():获取条数
->db.users.update({"username":"Linyongzhi"},{$set{"age":18}}),默认只修改第一条
->db.users.update({"username":"Linyongzhi"},{$set{"age":18}},{multi:true}):可以修改多条
->db.users.save(),也可以进行修改
->db.users.remove({"username":"Zhangxiaojin"}):删除所有满足条件的行
->db.users.remove({"username":"Zhangxiaojin"},true):删除满足条件的第一行
->db.users.remove({}):删除所有的行
->db.users.drop():删除所有的行,包括索引
二.使用Mongose模块操作MongoDB
1.连接数据库兵创建Schema:
var mongoose = require('mongoose')
var uri = 'mongodb://username:password@hostname:port/databasename';
uri = 'mongodb://localhost/part9';
mongoose.connect(uri);
var BookSchema = new mongoose.Schema({
name: String,
author: String,
publishTime : Date
});
mongoose.model('Book',BookSchema);
2.插入数据
var mongoose = require('mongoose')
require('./model.js');
var Book = mongoose.model('Book');
var book = new Book({
name : "MEAN Web",
author : "Green",
publishTime: new Date()
});
book.author = 'Jim';
book.save(function(err){
console.log('sava status:' , err? 'failed' : 'success' );
});
3.查找数据
var mongoose = require('mongoose')
require('./model.js');
var Book = mongoose.model('Book');
Book.find({},function(err,docs){
if(err){
console.log('err',err);
return;
}
console.log('result:'+docs);
})
--------------------------------
//查找一条记录
var mongoose = require('mongoose')
require('./model.js');
var Book = mongoose.model('Book');
Book.findOne({name:"Jim"},function(err,doc){
if(err){
console.log('err',err);
return;
}
doc.name = 'Node.js';
doc.save();
console.log('result:'+doc);
})
4.删除数据
var mongoose = require('mongoose')
require('./model.js');
var Book = mongoose.model('Book');
Book.findOne({name:"Jim"},function(err,doc){
if(err){
console.log('err',err);
return;
}
doc.remove();
console.log('result:'+doc);
});
5.条件查询
var mongoose = require('mongoose')
require('./model.js');
var Book = mongoose.model('Book');
var cond = {
$or : [
{name:'Jim'},
{name:'Node.js'}
]
}
Book.find(cond,function(err,docs){
if(err){
console.log('err',err);
return;
}
console.log('cond:',cond,'result:',docs);
})
三.在Express中使用MongoDB
项目的结构:
1.config.js
module.exports = {
mongodb : "mongodb://localhost/part9"
};
2.mongoose.js
var mongoose = require('mongoose');
var config = require('./config.js');
module.exports = function(){
var db = mongoose.connect(config.mongodb);
require('../models/user.server.model.js');
return db;
}
3.user.server.model.js
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
uid : Number,
username : String,
createTime : Date,
lastLogin : Date
});
mongoose.model('User',UserSchema);
}
4.user.js
//...
var mongoose = require('mongoose');
var User = mongoose.model('User');
router.get('/test', function(req, res, next) {
var user = new User({
uid : 1,
username : 'Linyongzhi'
});
//保存操作
user.save(function(err){
if(err){
res.end('Error');
return next();
}
User.find({},function(err,docs){
if(err){
res.end('Error');
return next();
}
res.json(docs);
});
});
});