一、启动
1、 启动mongodb数据库服务,如果数据库已经被注册Windows自带的服务,并且服务在开机的时候,已经启动,这一步可以省略
2、 到mongodb文件存放文件夹,并在进入bin目录,运行mongo命令如下:
E:\nosql\mongodb\bin>mongo 127.0.0.1:9988
MongoDB shell version: 2.0.3-rc1
connecting to: 127.0.0.1:9988/myDbtest
这样表示正常进入mongodb服务
如果数据库的连接端口是默认端口(27017),则不需要书写地址和端口,直接连接要访问的实例(集合集)
可以使用mongo --help 指令来查看详细的参数说明
E:\nosql\mongodb\bin>mongo --help
MongoDB shell version: 2.0.3-rc1
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no 'db address'
arg expected
--norc will not run the ".mongorc.js" file on start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)
二、命令
1、创建和插入
创建一个集合(关系数据库中的表),并向集合中插入一条数据,直接使用插入指令insert,在执行insert指令的时候,如果数据库中没有集合,数据库会自动创建一个集合,如果有,就直接插入,如:
> db.animals.insert({name:"tiger",sex:"male",type:"east north tiger"})
创建一张表animals,表的字段有name,sex,type三个字段,并给这三个字段插入相应的值
3、 查询
我们可以查询我们刚插入的表是否有成功,执行如下指令:
> db.animals.find()
返回的结果
{ "_id" : ObjectId("4f54cc7cf79fe0276bb21c7d"), "name" : "tiger", "sex" : "male", "type" : "east north tiger" }
出现上面的结果,我们可以看到,这就是我们刚插入额值,多了一个_id字段,这是mongodb数据库自动生成,mongodb数据库是使用bson数据格式现实的,这是一种和json一样的数据格式