在 Ubuntu14.04 上安装
Install MongoDB Community Edition
1. Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
2. Create a list file for MongoDB.
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
3. Reload local package database.
sudo apt-get update
4. Install the MongoDB packages.
Install the latest stable version of MongoDB.
sudo apt-get install -y mongodb-org
使用
1. Start MongoDB.
sudo service mongod start
2. Verify that MongoDB has started successfully
Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading
[initandlisten] waiting for connections on port
3. Stop MongoDB.
sudo service mongod stop
4. Restart MongoDB.
sudo service mongod restart
关闭 mongodb 的守护进程
mongod --shutdown
卸载
Uninstall MongoDB Community Edition
1. Stop MongoDB.
sudo service mongod stop
2. Remove Packages.
sudo apt-get purge mongodb-org*
3. Remove Data Drections.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
在 windows 上安装
查看当前windows的版本
wmic os get caption
Caption
Microsoft Windows 10 专业版
wmic os get osarchitecture
OSArchitecture
64 位
安装路径
C:\myprogram\MongoDB\3.6
自定义脚本
mongo.bat
d:
cd C:\myprogram\MongoDB\3.6\bin
mongod.exe --dbpath "G:\mongodata\db"
mongo-cli.bat
d:
cd C:\myprogram\MongoDB\3.6\bin
mongo.exe
MongoDB操作总结
1 创建数据库
use mydb//没有就创建
2 显示数据库
show dbs
3 显示数据库的状态
db.stats()
4 检查当前所用的数据库
db
5 删除数据库(先用然后删除)
use mydb
db.dropDatabase()
6 在数据库中创建集合(mycol为集合名称)
a. db.createCollection('mycol') //创建默认集合
db.mycol.insert({id:1,name:'张三',age:20})
db.mycol.find({})
db.mycol.insert({_id:'aa',name:'李四',age:20})
b. db.createCollection("mycol",{capped:true,autoIndexID:true,size:6142800,max:10000}) //创建指定参数集合
指定参数:
capped true 启用封顶集合,固定大小的集合,会自动覆盖最早的条目,如果true,也需要指定尺寸参数
size number 指定最大字节封顶集合,如果封顶为true,需要指定这个字段
max number 指定封顶集合最大数量,最高多少条记录
autoIndexID true 则_id字段自动创建索引,默认false,慎用false,因为会导致同步数据时负载很高,建议true
7 直接插入数据创建集合
db.teacher.insert({'draven':'node'})
//自动创建名为teacher的数据集,里面有一条键值对
8 显示集合
show collections
9 删除集合
db.mycol.drop()
10 删除集合数据
db.mycol.remove({})
11 显示表格记录
db.mycol.find({})
数据类型和嵌套关联
//多条插入
db.mycol.insert([{},{},{}])
//覆盖保存,没有则插入,有则覆盖
db.user.save({})
//插入时间
db.test.insert({time:new Date()})
给mongodb插入日期的数据时发现,日期时间相差8小时,原来存储在mongodb中的时间UTC+0:00,而中国的时区是+