Ubuntu下MongoDB的安装和使用

Ubuntu下MongoDB的安装和使用简介

1. MongoDB安装

1.1安装

MongoDB的安装,无需下载源文件,可以直接用apt-get命令进行安装。
打开终端,输入以下命令:

sudo apt-get install mongodb

1.2 查看版本

mongo -version
root@python:~# mongo -version
MongoDB shell version: 2.6.10

输出版本号表明安装成功。

1.3 服务启动与停止

默认设置MongoDB是随Ubuntu启动自动启动的。
输入以下命令查看是否启动成功:

pgrep mongo -l   #注意:-l是英文字母l
root@python:~# pgrep mongo -l
10763 mongod

启动、关闭和卸载mongodb命令如下:

service mongodb start
service mongodb stop
sudo apt-get --purge remove mongodb mongodb-clients mongodb-server

2. 使用MongoDB

2.1 登录数据库

在终端输入mongo进入数据库,默认连接的数据库是test数据库,在此之前一定要确保你已经启动了MongoDB,否则会出现错误,启动之后运行成功,如下截图:

root@python:~# mongo
MongoDB shell version: 2.6.10
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> 

2.2 常用操作命令:

*数据库相关 :

show dbs:显示数据库列表
show collections:显示当前数据库中的集合(类似关系数据库中的表table)
show users:显示所有用户
use TestDB:切换当前数据库至TestDB
db.help() :显示数据库操作命令
db.TestCollection.help() :显示集合操作命令,TestCollection是集合名
MongoDB没有创建数据库的命令,如果你想创建一个“Class”的数据库,先运行use Class命令,之后做一些操作(如:创建聚集集合db.createCollection(‘Student’)),这样就可以创建一个名叫“Class”的数据库.

详细讲解:

2.2.1 创建一个数据库:

USE[databaseName]
但是你什么也不干就离开的话这个空数据库就会被删除

2.2.2 查看所有的数据库

show dbs

2.2.3 给指定数据库添加集合并且添加记录

db.[documentName].insert({…})

数据库的插入

ues foobar
> db.persons.insert({"name":"zbb"})
> show dbs
foobar  0.03125GB
local   (empty)
>

2.2.4 查看数据库中的所有 文档

show collections

2.2.5 查询指定文档的数据

查询所有db.[documentName].find()
查询第一条数据 db.[documentName].findOne()
> db.persons.find()
{ "_id" : ObjectId("5ab62da8177815090230ad34"), "name" : "zbb" }
> db.persons.findOne()
{ "_id" : ObjectId("5ab62da8177815090230ad34"), "name" : "zbb" }
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "foobar.persons", "name" : "_id_" }
> db.system.indexes.findOne()
{
        "v" : 1,
        "key" : {
                "_id" : 1
        },
        "ns" : "foobar.persons",
        "name" : "_id_"
}

2.2.6 更新文档数据

db.[documentName].update({查询条件},{更新内容})

var p = db.persons.findOne()
db.perosns.update(p,{name:"zbb123"})

db.persons.update({name:"binbin1"},{$set:{name:"erices"}})


> db.persons.insert({name:"binbin1"})
> db.persons.insert({name:"binbin2"})
> db.persons.find()
{ "_id" : ObjectId("5ab62da8177815090230ad34"), "name" : "zbb" }
{ "_id" : ObjectId("5ab630ee177815090230ad35"), "name" : "binbin1" }
{ "_id" : ObjectId("5ab630f2177815090230ad36"), "name" : "binbin2" }
> db.persons.update({name:"binbin1"},{$set:{name:"erices"}})
> db.persons.find()
{ "_id" : ObjectId("5ab62da8177815090230ad34"), "name" : "zbb" }
{ "_id" : ObjectId("5ab630ee177815090230ad35"), "name" : "erices" }
{ "_id" : ObjectId("5ab630f2177815090230ad36"), "name" : "binbin2" }
>

2.2.7 删除文档的数据

db.[documentName].remove({...})
db.persons.remove({"name":"zbb"})

2.2.8 删除库中的集合

db.persons.drop()
db.[documentName].drop()

> db.persons.drop()
true
> show collections
system.indexes 
>

2.2.9 删除数据库

db.dropDatabase()   #注意dropDatabase()中D需要大写

> show dbs
foobar  0.03125GB
local   (empty)
> db.dropDatabase()
{ "dropped" : "foobar", "ok" : 1 }
> show dbs
local   (empty)

2.2.10 shell的help

db.help()
db.persons.help()

2.2.11 mongoDB的API

http://api.mongodb.com/js/3.3.11/
http://api.mongodb.com/js/3.3.11/

2.2.12 数据库和集合命名规范:

不能是空字符串
不得含有’ ‘(空格) , $ / \ \o(空字符)
应该全部小写
最多64个字节
数据库名不能与现有系统保留库同名 admin local config

这样的数据库也是合法的:
db-text但是不能通过db.[documentName]得到了
要通过db.getCollection(documentName) 因为db-text会被当做减法操作

mongoDB的shell内置javas引擎可以执行js代码
function insert(objext){
db.getcollection(“db-text”).text.insert(object)
#db.getcollection(“db-text”)得到数据库的名称 text集合 insert插入操作
}

BSON是JSON的扩展他新增加了诸如日期,浮点等json不支持的数据类型

猜你喜欢

转载自blog.csdn.net/erice_s/article/details/80281577