您的位置:首页 > 数据库 > Mongodb

MongoDB入门示例及介绍

2013-08-22 15:56 489 查看
1、MongoDB介绍

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:

* 面向集合存储,易存储对象类型的数据。

* 模式自由。

* 支持动态查询。

* 支持完全索引,包含内部对象。

* 支持查询。

* 支持复制和故障恢复。

* 使用高效的二进制数据存储,包括大型对象(如视频等)。

* 自动处理碎片,以支持云计算层次的扩展性

* 支持RUBY,PYTHON,J***A,C++,PHP等多种语言。

* 文件存储格式为BSON(一种JSON的扩展)

* 可通过网络访问

所谓“面向集合”(Collenction-Orented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。每个 集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定 义任何模式(schema)。

模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。

存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized document Format)。

MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDB在32位模式运行时支持的最大文件尺寸为2GB。

MongoDB把数据存储在文件中(默认路径为:/data/db,该目录必须存在,否则启动的时候会报错),为提高效率使用内存映射文件进行管理。

以上内容摘自:http://www.oschina.net/p/mongodb

2、下载及安装

下载地址:http://www.mongodb.org/downloads,选择合适的版本。

安装就是将下载的文件解压到你想存放的指定的目录即可,相比较于其它需要安装的数据库,这个简直是太简单了。

3、文件及目录介绍

在Windows下面,mongodb就只有一个bin目录以及bin目录以外的三个文件,相对bin目录中包括了如下文件:

bsondump.exe 用于将导出的BSON文件格式转换为JSON格式

mongo.exe mongoDB的客户端

mongod.exe 用于启动mongoDB的Server

mongod.pdb

mongodump.exe 用于从mongodb数据库中导出BSON格式的文件,类似于mysql的dump工具mysqldump

mongoexport.exe 用于将mongodb中的数据库,导出为JSON,CSV或TSV的格式。使用示例:mongoexport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts

mongofiles.exe 用于和mongoDB的GridFS文件系统交互的命令,并可操作其中的文件,它提供了我们本地系统与GridFS文件系统之间的存储对象接口。使用示例:mongofiles --hostname db1.example.net --port 37017 -d records list

mongoimport.exe 用于将JSON,CSV或TSV等文件格式,导入到mongoDB数据库中,使用示例:mongoimport --db users --collection contacts --type csv --file /opt/backups/contacts.csv

mongooplog.exe 用于从运行的mongod服务中拷贝运行日志到指定的服务器,主要用于增量备份,使用示例:mongooplog --from mongodb0.example.net --host mongodb1.example.net,备份还可以参看一下这篇BLOG:http://www.ttlsa.com/html/2052.html

mongoperf.exe 用于独立检查mongoDB的I/O性能的工具,使用示例:mongoperf.exe < testPrefJson.txt (注:testPrefJson.txt存放测试的参数,以json格式,如:{nThreads:16,fileSizeMB:1000,r:true},更多参数参看该命令的帮助),在linux上配置iostat命令观察写入的效果

mongorestore.exe用于恢复导出的BSON文件到mongodb数据库中

mongos.exe 用于注册系统处理

mongos.pdb

mongostat.exe 当前mongod状态监控工具,像linux中监控linux的vmstat

mongotop.exe 提供了一个跟踪mongod数据库花费在读写数据的时间,为每个collection都会记录,默认记录时间是按秒记录。

mongosniff (linux及unix有此工具)用于监控连接到mongodb的TCP/IP连接,类似于tcpdump。应用可以查看这篇BLOG:http://blog.nosqlfan.com/html/521.html

注:每个命令都可以通过参数"--help"获取帮助,不过更好的是阅读mongodb的manual,里面都有介绍。

4、启动服务端与客户

启动服务端:

运行命令:mongod.exe (注:此目录/data/db必须存在)

启动客户端:

运行命令:mongo.exe

5、CRUD示例

以下是示例,是根据mongoDB官网的online shell完成的

3. Saving
Here's how you save a document to MongoDB:
  db.scores.save({a: 99}); 

This says, "save the document '{a: 99}' to the 'scores' collection."
Go ahead and try it. Then, to see if the document was saved, try
  db.scores.find(); 
Once you've tried this, type 'next'.

>db.scores.save({a:99});
"ok"
>db.me.save({name:'tom',language:['java','c','python']});
"ok"
>db.me.find();

[ 
  {   "name" : "tom",   "language" : [   "java",   "c",   "python" ],   "_id" : {   "$oid" : "52156671cc93742c160c66bc"   }   }
]
>db.scores.find();

[ 
  {   "a" : 99,   "_id" : {   "$oid" : "52156646cc93742c160c66bb"   }   }
]
>
4. Saving and Querying
Try adding some documents to the scores collection:
  for(i=0; i<10; i++) { db.scores.save({a: i, exam: 5}) }; 

Try that, then enter
  db.scores.find(); 
to see if the save succeeded. Since the shell only displays 10 results at time,
you'll need to enter the 'it' command to iterate over the rest.

(enter 'next' when you're ready)
>for(i=0;i<20;i++){db.scores.save({student:i,score:20})};
"ok"
>db.scores.find();

[ 
  {   "a" : 99,   "_id" : {   "$oid" : "52156646cc93742c160c66bb"   }   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c0"   },   "student" : 3,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c1"   },   "student" : 1,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c2"   },   "student" : 0,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c3"   },   "student" : 2,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c4"   },   "student" : 5,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c5"   },   "student" : 4,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c6"   },   "student" : 7,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c7"   },   "student" : 6,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c8"   },   "student" : 9,   "score" : 20   }
]
>it

[ 
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c9"   },   "student" : 10,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66ca"   },   "student" : 11,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66cb"   },   "student" : 12,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66cc"   },   "student" : 13,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66cd"   },   "student" : 14,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66ce"   },   "student" : 8,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66cf"   },   "student" : 15,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d0"   },   "student" : 16,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d1"   },   "student" : 17,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d2"   },   "student" : 19,   "score" : 20   }
]
>it

[ 
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d3"   },   "student" : 18,   "score" : 20   }
]
>
5. Basic Queries
You've already tried a few queries, but let's make them more specific.
How about finding all documents where a == 2:
  db.scores.find({a: 2}); 

Or what about documents where a > 15?
  db.scores.find({a: {'$gt': 15}}); 

>db.scores.find({student:2});

[ 
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c3"   },   "student" : 2,   "score" : 20   }
]
>db.scores.find({a:{'$gt':12}});

[ 
  {   "a" : 99,   "_id" : {   "$oid" : "52156646cc93742c160c66bb"   }   }
]
>db.scores.find({student:{'$gt':12}});

[ 
  {   "_id" : {   "$oid" : "52156721cc93742c160c66cc"   },   "student" : 13,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66cd"   },   "student" : 14,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66cf"   },   "student" : 15,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d0"   },   "student" : 16,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d1"   },   "student" : 17,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d2"   },   "student" : 19,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156721cc93742c160c66d3"   },   "student" : 18,   "score" : 20   }
]
>db.scores.find({student:{'$lt':12}});

[ 
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c0"   },   "student" : 3,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c1"   },   "student" : 1,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c2"   },   "student" : 0,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c3"   },   "student" : 2,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c4"   },   "student" : 5,   "score" : 20   },
  {   "_id" : {   "$oid" : "5215671fcc93742c160c66c5"   },   "student" : 4,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c6"   },   "student" : 7,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c7"   },   "student" : 6,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c8"   },   "student" : 9,   "score" : 20   },
  {   "_id" : {   "$oid" : "52156720cc93742c160c66c9"   },   "student" : 10,   "score" : 20   }
]
>db.scores.find({student:{'$eq':12}});

[ 
  
]
>
6. Query Operators
Query Operators:
$gt is one of many special query operators. Here are few others:
  $lt  - '<',   $lte - '<=', 
  $gte - '>=',  $ne  - '!='
  $in - 'is in array',  $nin - '! in array'

db.scores.find({a: {'$in': [2, 3, 4]}}); 
db.scores.find({a: {'$gte': 2, '$lte': 4}}); 
Try creating some queries, then type 'next.'

>db.scores.find({student:{'$in':[12]}});

[ 
  {   "_id" : {   "$oid" : "52156720cc93742c160c66cb"   },   "student" : 12,   "score" : 20   }
]
>db.scores.save({student:'tom',age:15,language:['java','c','ruby']});
"ok"
>db.scores.save({student:'tom1',age:16,language:['java','c','python']});
"ok"
>db.scores.save({student:'tom2',age:16,language:['c','python']});
"ok"
>db.scores.find({language:{'$in':['c']}});

[ 
  {   "language" : [   "java",   "c",   "ruby" ],   "_id" : {   "$oid" : "52156a7ecc93742c160c66da"   },   "student" : "tom",   "age" : 15   },
  {   "language" : [   "java",   "c",   "python" ],   "_id" : {   "$oid" : "52156a8fcc93742c160c66db"   },   "student" : "tom1",   "age" : 16   },
  {   "language" : [   "c",   "python" ],   "_id" : {   "$oid" : "52156a9dcc93742c160c66dc"   },   "student" : "tom2",   "age" : 16   }
]
>db.scores.find({language:{'$in':['python']}});

[ 
  {   "language" : [   "java",   "c",   "python" ],   "_id" : {   "$oid" : "52156a8fcc93742c160c66db"   },   "student" : "tom1",   "age" : 16   },
  {   "language" : [   "c",   "python" ],   "_id" : {   "$oid" : "52156a9dcc93742c160c66dc"   },   "student" : "tom2",   "age" : 16   }
]
>
7. Updates
Now create a couple documents like these for updating:
  db.users.save({name: 'Johnny', languages: ['ruby', 'c']}); 
  db.users.save({name: 'Sue', languages: ['scala', 'lisp']}); 
Make sure they were saved by called db.users.find()
Update the first document like so:
  db.users.update({name: 'Johnny'}, {name: 'Cash', languages: ['english']}); 
>db.users.save({name:'Johnny',language:['java','c']});
"ok"
>db.users.save({name:'Tome',language:['python','java']});
"ok"
>db.users.find();

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Tome",   "language" : [   "python",   "java" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   }   }
]
>db.users.find({name:'Tome'});

[ 
  {   "name" : "Tome",   "language" : [   "python",   "java" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   }   }
]

>db.users.update({name:'Tome'},{name:'Jack',language:['english']});
"ok"
>db.users.find();

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Jack",   "language" : [   "english" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   }   }
]
>
8. Update Operators
The previous update replaced the entire document, but MongoDB also
supports partial updates to documents. For example, you can set a value:
  db.users.update({name: 'Cash'}, {'$set': {'age': 50} }); 
You can also push and pull items from arrays:
  db.users.update({name: 'Sue'}, {'$pull': {'languages': 'scala'} }); 
  db.users.update({name: 'Sue'}, {'$push': {'languages': 'ruby'} }); 
Give these a try, check the results, and then enter 'next'.
>db.users.update({name:'Jack'},{'$set':{'age':50}});
"ok"
>db.users.find();

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Jack",   "language" : [   "english" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   },   "age" : 50   }
]

>db.users.update({name:'Jack'},{'$pull':{language:'java'}});
"ok"
>db.users.find();

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Jack",   "language" : [   "english" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   },   "age" : 50   }
]
>db.users.find();
"ok"
>db.users.update({name:'Jack'},{'$pull':{language:'java'}});

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Jack",   "language" : [   "english",   "java" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   },   "age" : 50   }
]
>db.users.find();
"ok"
>db.users.update({name:'Jack'},{'$set':{age:60}});

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Jack",   "language" : [   "english" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   },   "age" : 50   }
]
>db.users.update({name:'Jack'},{'$set':{age:60}});
"ok"
>db.users.find();

[ 
  {   "name" : "Johnny",   "language" : [   "java",   "c" ],   "_id" : {   "$oid" : "52156b66cc93742c160c66df"   }   },
  {   "name" : "Jack",   "language" : [   "english" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   },   "age" : 60   }
]
>
9. Deleting data
To delete matching documents only, add a query selector to the remove method:
  db.users.remove({name: 'Sue'});
To delete everything from a collection:
  db.scores.remove();
>db.users.remove({name:'Johnny'});
"ok"
>db.users.find();

[ 
  {   "name" : "Jack",   "language" : [   "english" ],   "_id" : {   "$oid" : "52156b8bcc93742c160c66e2"   },   "age" : 60   }
]
>db.users.remove();
"ok"
>db.users.find();

[ 
  
]


6、MongoDB与RDBMS的比较与使用场景

不要简单的拿MongoDB与其它的RDBMS进行性能读写的场景比较,如mysql中的一个单表带索引的查询,与将这些数据放到mongoDB中,来比较谁的查询效率会更高,这种比较是没有意义的,就像飞机、轮船,汽车和自行车比较谁更快一样,如果是在很短的路程范围之内,是无法比较的,并且他们各自有各自的应用场景,在各处的应用场景中都能够发挥出很好的性能。MongoDB专注的是分布式、大容易存储,在大对象的处理处理会更有优势,如一个很复杂的对象,如果是存入到RDBMS中,可能会分为几个甚至于10几个表,查询数据的时候就会根据主外键到这10几个表中去关联数据;而MongoDB是基于是文档对象,存入到MongoDB中只需要放在一个collection中即可,查询的时候也只需要一下就全部查出来了,当然这只是一个方面而已。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: