win7下首次使用mongodb

win7下首次使用mongodb

http://blog.csdn.net/qingsong3333/article/details/62237894

原创 2017年03月15日 21:36:09

本文记录了第一次在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

 以管理员身份打开命令行管理器,

 

[plain] view plain copy
 
  1. C:\windows\system32> cd C:\mongodb\bin  
  2.   
  3. C:\mongodb\bin> mongod.exe --dbpath  C:\mongodb\data\db --logpath C:\mongodb\data\mongodb.log  
  4. Wed Mar 15 16:14:23.683  
  5. 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.  
  6. Wed Mar 15 16:14:23.683  
  7. all output going to: C:\mongodb\data\mongodb.log  
  8. 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.

 

 

[plain] view plain copy
 
  1. C:\windows\system32>cd C:\mongodb\bin  
  2.   
  3. C:\mongodb\bin>mongo  
  4. MongoDB shell version: 2.4.5  
  5. connecting to: test  
  6. Server has startup warnings:  
  7. Wed Mar 15 16:14:23.703 [initandlisten]  
  8. Wed Mar 15 16:14:23.703 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.  
  9. Wed Mar 15 16:14:23.703 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).  
  10. Wed Mar 15 16:14:23.703 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.  
  11. Wed Mar 15 16:14:23.703 [initandlisten] **       See http://dochub.mongodb.org/core/32bit  
  12. Wed Mar 15 16:14:23.703 [initandlisten]  
  13. > use test2  
  14. switched to db test2  
  15. > use test1  
  16. switched to db test1  

 

4. 创建集合

 

切换回数据库test1,创建集合teacher, student,并查看创建的集合

 

[plain] view plain copy
 
  1. > db.createCollection("teacher")  
  2. { "ok" : 1 }  
  3. > db.createCollection("student")  
  4. { "ok" : 1 }  
  5. > show collections  
  6. student  
  7. system.indexes  
  8. teacher  

 

 

5. 插入文档

往teacher里插入两条文档。如果集合不存在的情况下,直接插入文档,那么会自动创建该集合。

 

[plain] view plain copy
 
  1. > db.teacher.insert({name:'Wang', class:10, course:'math', name:'Li'})  
  2. > db.teacher.insert({name:'Li', age:36, location:'Beijing', course:'English'})  
  3. > db.teacher.find()  
  4. { "_id" : ObjectId("58c9010e6e493de32325d647"), "name" : "Li", "class" : 10, "course" : "math" }  
  5. { "_id" : ObjectId("58c901206e493de32325d648"), "name" : "Li", "age" : 36, "location" : "Beijing", "course" : "English"}  
  6. > db.teacher.find().pretty()  
  7. {  
  8.         "_id" : ObjectId("58c9010e6e493de32325d647"),  
  9.         "name" : "Li",  
  10.         "class" : 10,  
  11.         "course" : "math"  
  12. }  
  13. {  
  14.         "_id" : ObjectId("58c901206e493de32325d648"),  
  15.         "name" : "Li",  
  16.         "age" : 36,  
  17.         "location" : "Beijing",  
  18.         "course" : "English"  
  19. }  

 

6. 删除文档

将英语课老师的记录删掉

 

 

[plain] view plain copy
 
  1. > db.teacher.remove({"course":"English"})  
  2. > db.teacher.find()  
  3. { "_id" : ObjectId("58c902ef6e493de32325d647"), "name" : "Li", "class" : 10, "course" : "math" }  
  4. >  

 

 

7. 删除集合

 

[plain] view plain copy
 
  1. > db.teacher.drop()  
  2. true  
  3. > db.student.drop()  
  4. true  
  5. > show collections  
  6. system.indexes  

 

 

8. 查看数据库 

由于数据库test2中并未创建任何集合,因此不显示

 

[plain] view plain copy
 
  1. > show dbs  
  2. local   0.03125GB  
  3. test1   0.0625GB  

 

 

9. 删除数据库

 

[plain] view plain copy
 
  1. > db.dropDatabase()  
  2. { "dropped" : "test1", "ok" : 1 }  
  3. > show dbs  
  4. local   0.03125GB  

 

 

10. 停止mongodb

 

[plain] view plain copy
 
  1. > db.shutdownServer()  
  2. shutdown command only works with the admin database; try 'use admin'  
  3. > use admin  
  4. switched to db admin  
  5. > db.shutdownServer()  
  6. Wed Mar 15 17:12:38.944 DBClientCursor::init call() failed  
  7. server should be down...  
  8. Wed Mar 15 17:12:38.944 trying reconnect to 127.0.0.1:27017  
  9. Wed Mar 15 17:12:39.954 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017  

 

版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/qingsong3333/article/details/62237894

 

posted @ 2018-03-16 15:37  sky20080101  阅读(106)  评论(0)    收藏  举报