mongodb的基本语法

一、启动shell:(主要用crt 软件的时候终端要选择linux,不然不能退格键有时候出问题)linux

[root@saltstack mongodb]# mongomongodb

> show dbs   #查看数据库
admin     (empty)
local     0.078GB
modbtest  0.078GB
myinfo    (empty)
test      (empty)
xiaoluo   0.078GBshell

> use dbtest    #使用数据库,没有的话会自动建立
switched to db dbtest数据库

#建立一个字典文档,是以key,value方式存储的:
ide

> test = {'id':1,'name':'xiaoming','job':'it'}
{ "id" : 1, "name" : "xiaoming", "job" : "it" }rem

> test
{ "id" : 1, "name" : "xiaoming", "job" : "it" }文档

#自动建立一个叫dbtest的表,而后往里面插入test这个字典:
it

> db.dbtest.insert(test)
WriteResult({ "nInserted" : 1 })
#或者能够手工插入数据:
> db.dbtest.insert({'id':2,'name':'xiaoli','job':'it'})
WriteResult({ "nInserted" : 1 })table

#查看数据使用find的方法:
class

> db.dbtest.find()
{ "_id" : ObjectId("5513375e83aef55e0cc2d05c"), "id" : 1, "name" : "xiaoming", "job" : "it" }
{ "_id" : ObjectId("5513379383aef55e0cc2d05d"), "id" : 2, "name" : "xiaoli", "job" : "it" }
> show tables;
dbtest
system.indexes

#根据条件查找

> db.dbtest.find({'id':1})
{ "_id" : ObjectId("5513375e83aef55e0cc2d05c"), "id" : 1, "name" : "xiaoming", "job" : "it" }

#根据条件进行更新里面的选型

> db.dbtest.update({'id':2},{'id':2,'name':'xiaoluo','job':'it'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dbtest.find({'id':2})
{ "_id" : ObjectId("5513379383aef55e0cc2d05d"), "id" : 2, "name" : "xiaoluo", "job" : "it" }
>
#根据条件进行删除使用remove方法。删除id=2的字典:

> db.dbtest.remove({'id':2})WriteResult({ "nRemoved" : 1 })> db.dbtest.find(){ "_id" : ObjectId("5513375e83aef55e0cc2d05c"), "id" : 1, "name" : "xiaoming", "job" : "it" }

相关文章
相关标签/搜索