准备
- 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-3.2.10.tgz
- 解压
tar -xvf mongodb-linux-x86_64-ubuntu1604-3.2.10.tgz
- 将mongodb中的bin 加入 $PATH
第一部分:mongod 的配置
mongod是mongodb主要的后台进程,它用来处理数据请求,管理数据存取,执行后台管理操作。在平常的使用中,一般通过一个config文件来管理数据库的行为。
- 简单的配置
** myblog.conf**
port = 27017
dbpath = db
logpath = log/myblog.txt
rest = true
- 启动 mongd
sudo mongod --config myblog.conf
加入 --fork 进行后台运行
是在那个配置文件的同一个目录中
第二部分:mongodb shell 的命令
1、查询本地所有数据库名称
show dbs;
2、切换至指定数据库环境(若无指定的数据库,则创建新的库)
use mydb;
3、查询当前库下的所有聚集集合collection(相当于table)
show collections;
4、创建聚集集合
db.createCollection('mycollection');
5、查询聚集集合中数据条数
db.mycollection.count();
6、插入数据
db.mycollection.insert({'username':'xyz_lmn','age':26,'salary':120});
往'mycollection'聚集集合中插上一条数库,name为'xyz_lmn',age为'26',salary为'120'
7、查询age等于26的数据
db.mycollection.find({"age":26});
8、查询salary大于100的数据
db.mycollection.find({salary:{$gt:100}});
9、查询age小于30,salary大于100的数据
db.mycollection.find({age:{$lt:30}},{salary:{$gt:100}});
10、查询salary小于40或salary大于200的数据
db.mycollection.find({$or: [{salary: {$lt:40}}, {salary: {$gt:200}}]});
11、查询指定列的数据
db.mycollection.find({},{age:1,salary:1});
1表示显示此列的意思,也可以用true表示 12、查询username中包含'e'的数据
db.mycollection.find({username:/e/});
13、查询以a打头的数据
db.mycollection.find({username:/^a/});
14、查询age列数据,并去掉重复数据
db.mycollection.distinct('age');
15、查询前10条数据
db.mycollection.find().limit(10);
16、查询1条以后的所有数据
db.mycollection.find().skip(1);
17、查询第一条数据
db.mycollection.findOne();
18、查询结果集的记录数(查询salary小于40或大于100的记录数)
db.mycollection.find({$or: [{salary: {$lt:40}}, {salary: {$gt:100}}]}).count();
19、按salary升序排序
db.mycollection.find().sort({salary:1});
按照salary字段升序排序
20、降序
db.mycollection.find().sort({salary:-1});
按照salary字段降序排序
21、根据username修改age
db.employee.update({username:'jim'},{$set:{age:22}},false,true);
db.collection.update( criteria, objNew, upsert, multi )
criteria : update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
22、将指定username的age字段增加5
db.mycollection.update({username:'jim'},{$inc:{age:5}},false,true);
将username为‘jim’的age字段加5
23、删除username为'rose'的数据
db.mycollection.remove({uname:'rose'});
24、集合collection重命名
db.mycollection.renameCollection('c_temp');
将mycollection集合重命名为'c_temp'
25、删除集合
db.c_temp.drop();
删除名为'c_temp'的集合
26、删除当前数据库
db.dropDatabase();
第三部分:express 中操作mongodb数据库的一些要点
- db.js
var settings = require('../settings.js');
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
module.exports = new Db(settings.db, new Server(settings.host, settings.port), {safe: true});
- user.js
var mongodb = require('./db');
var crypto = require('crypto');
function User(user) {
this.username = user.username;
this.password = user.password;
this.email = user.email;
}
User.prototype.get = function(username, callback) {
mongodb.open(function(err, db) {
if (err) {
return callback(err);
};
db.collection('users', function(err, collection) {
if (err) {
mongodb.close();
return callback(err);
};
collection.findOne({
username: username
}, function(err, data) {
if (err) {
mongodb.close();
};
callback(null,data)
})
})
})
}
User.prototype.save = function(callback) {
var user = {
username: this.username,
password: this.password,
email: this.email
};
mongodb.open(function(err, db) {
if (err) {
return callback(err);
};
db.collection('users', function(err, collection) {
if (err) {
mongodb.close();
return callback(err);
};
collection.insert(user, {
safe: true
}, function(err, data) {
mongodb.close();
if (err) {
return callback(err);
};
callback(null, data)
});
});
});
};
module.exports = User;
- route.js
app.post('/signup',function(req, res){
var newUser = new User({
username: req.body.username,
password: req.body.password,
email: req.body.email
});
newUser.get(newUser.username, function(err, user){
if (user) {
res.send(user)
}else {
newUser.save(function(err, result){
if (err) {
res.send(err)
}else {
res.send(result)
}
})
}
})
})