本文记录了第一次在win7上使用mongodb的经历,主要包括mongodb的启、停,数据库的创建、查看、删除,集合的创建、删除,文档的插入、删除
1. 安装mongodb
下载 http://pan.baidu.com/s/1qYAVfLq建立如下文件夹 C:\mongodb, C:\mongodb\data, C:\mongodb\data\db,并将下载的mongodb解压,将整个bin目录拷贝到C:\mongodb下,并新建文件C:\mongodb\data\mongodb.log
2. 启动mongodb
以管理员身份打开命令行管理器,
C:\windows\system32> cd C:\mongodb\bin
C:\mongodb\bin> mongod.exe --dbpath C:\mongodb\data\db --logpath C:\mongodb\data\mongodb.log
Wed Mar 15 16:14:23.683
Wed Mar 15 16:14:23.683 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Wed Mar 15 16:14:23.683
all output going to: C:\mongodb\data\mongodb.log
log file [C:\mongodb\data\mongodb.log] exists; copied to temporary file [C:\mongodb\data\mongodb.log.2017-03-15T08-14-23]
3. 创建数据库
管理员身份新开一个CMD窗口, 创建两个数据库 test1, test2.C:\windows\system32>cd C:\mongodb\bin
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.5
connecting to: test
Server has startup warnings:
Wed Mar 15 16:14:23.703 [initandlisten]
Wed Mar 15 16:14:23.703 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Mar 15 16:14:23.703 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Mar 15 16:14:23.703 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Wed Mar 15 16:14:23.703 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Wed Mar 15 16:14:23.703 [initandlisten]
> use test2
switched to db test2
> use test1
switched to db test1
4. 创建集合
切换回数据库test1,创建集合teacher, student,并查看创建的集合
> db.createCollection("teacher")
{ "ok" : 1 }
> db.createCollection("student")
{ "ok" : 1 }
> show collections
student
system.indexes
teacher
5. 插入文档
往teacher里插入两条文档。如果集合不存在的情况下,直接插入文档,那么会自动创建该集合。> db.teacher.insert({name:'Wang', class:10, course:'math', name:'Li'})
> db.teacher.insert({name:'Li', age:36, location:'Beijing', course:'English'})
> db.teacher.find()
{ "_id" : ObjectId("58c9010e6e493de32325d647"), "name" : "Li", "class" : 10, "course" : "math" }
{ "_id" : ObjectId("58c901206e493de32325d648"), "name" : "Li", "age" : 36, "location" : "Beijing", "course" : "English"}
> db.teacher.find().pretty()
{
"_id" : ObjectId("58c9010e6e493de32325d647"),
"name" : "Li",
"class" : 10,
"course" : "math"
}
{
"_id" : ObjectId("58c901206e493de32325d648"),
"name" : "Li",
"age" : 36,
"location" : "Beijing",
"course" : "English"
}
6. 删除文档
将英语课老师的记录删掉> db.teacher.remove({"course":"English"})
> db.teacher.find()
{ "_id" : ObjectId("58c902ef6e493de32325d647"), "name" : "Li", "class" : 10, "course" : "math" }
>
7. 删除集合
> db.teacher.drop()
true
> db.student.drop()
true
> show collections
system.indexes
8. 查看数据库
由于数据库test2中并未创建任何集合,因此不显示> show dbs
local 0.03125GB
test1 0.0625GB
9. 删除数据库
> db.dropDatabase()
{ "dropped" : "test1", "ok" : 1 }
> show dbs
local 0.03125GB
10. 停止mongodb
> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
> use admin
switched to db admin
> db.shutdownServer()
Wed Mar 15 17:12:38.944 DBClientCursor::init call() failed
server should be down...
Wed Mar 15 17:12:38.944 trying reconnect to 127.0.0.1:27017
Wed Mar 15 17:12:39.954 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017