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

MongoDB Shell简单操作

2012-12-07 01:06 260 查看
1.连接数据库:数据库客户端shell操作,使用bin目录中的mongo文件,在mongo DB的安装中说是一个脚本,是不正确的,它是一个可执行的二进制文件:

连接入数据库如下:

[root@localhost bin]# ./mongo

MongoDB shell version: 2.2.2

connecting to: test

>

>

>

有点类似于mysql中的客户端bin目录中的mysql可执行文件

2.基本操作:

在步骤1中连接上数据库服务后,默认是连接到test这个数据库的,可以从步骤1的执行过程中看到。

1)使用help帮助命令:

> help

db.help() help on db methods

db.mycoll.help() help on collection methods

sh.help() sharding helpers

rs.help() replica set helpers

help admin administrative help

help connect connecting to a db help

help keys key shortcuts

help misc misc things to know

help mr mapreduce

show dbs show database names

show collections show collections in current database

show users show users in current database

show profile show most recent system.profile entries with time >= 1ms

show logs show the accessible logger names

show log [name] prints out the last segment of log in memory, 'global' is default

use <db_name> set current database

db.foo.find() list objects in collection foo

db.foo.find( { a : 1 } ) list objects in foo where a == 1

it result of the last line evaluated; use to further iterate

DBQuery.shellBatchSize = x set default number of items to display on shell

exit quit the mongo shell

>

可以看出可以在mongoDB中可以进行的一些操作。

2).数据库的查看:使用show dbs命令

> show dbs

local (empty)

test 0.0625GB

>

其中test数据库是在安装第一次接入mongodb的命令行时,使用show dbs命令,是不会看到test数据库的。

3).查看数据库中的集合(在mongodb中,集合就相当于关系型数据库中的表,在后面的文章中将具体说这个):

> show collections

c1

c2

system.indexes

>

看到以上结果,是因为在当前数据库中已经创建了c1和c2两个集合,其中还有一个创建集合的时候所生成的索引文件。

4).数据库数据查看(后面会具体写到数据库数据查询操作):

如查看test数据库中c1集合中的数据:

> db.c1.find();

{ "_id" : ObjectId("4fc14404703fa637a073651a"), "name" : "user2", "age" : 15 }

{ "_id" : ObjectId("4fc145e3703fa637a073651b"), "name" : "user3", "age" : 16 }

{ "_id" : ObjectId("4fc147a3703fa637a073651c"), "name" : "user4", "age" : 17 }

{ "_id" : ObjectId("4fc1c121b9137657a0d19cbd"), "name" : "user5", "age" : 22 }

{ "_id" : ObjectId("4fc1c121b9137657a0d19cbe"), "name" : "user6", "age" : 23 }

>

在mongodb的集合中存放的数据格式是json格式的数据,以key-value的形式,在mongodb中叫做bson数据格式。

5).添加数据:

如往c3集合中添加数据:> db.c3.insert({name:"kolle",age:14});添加数据的格式也是json格式的。

查看数据:

> db.c3.find();

{ "_id" : ObjectId("4fc3c93ceb59c2fe573dba71"), "name" : "kolle", "age" : 14 }

--------------------------------------------------------------------------------------------

以上是mongodb的简单操作包括:shell的使用,数据库的查看,集合查看,数据的简单插入和查询,后面对于各个方面讲详细讲解
本文出自 “unicorn” 博客,请务必保留此出处http://unicorn1842.blog.51cto.com/5640348/1081129
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: