32ew12 发表于 2015-3-27 09:54:14

mongodb的基本语法

1、启动shell:(主要用crt 软件的时候终端要选择linux,否则不能退格键有时候出问题)
# mongo
> show dbs   #查看数据库
admin   (empty)
local   0.078GB
modbtest0.078GB
myinfo    (empty)
test      (empty)
xiaoluo   0.078GB
> use dbtest    #使用数据库,没有的话会自动创建
switched to db dbtest
#创建一个字典文档,是以key,value方式存储的:

> test = {'id':1,'name':'xiaoming','job':'it'}
{ "id" : 1, "name" : "xiaoming", "job" : "it" }
> test
{ "id" : 1, "name" : "xiaoming", "job" : "it" }
#自动创建一个叫dbtest的表,然后往里面插入test这个字典:

> db.dbtest.insert(test)
WriteResult({ "nInserted" : 1 })
#或者可以手工插入数据:
> db.dbtest.insert({'id':2,'name':'xiaoli','job':'it'})
WriteResult({ "nInserted" : 1 })
#查看数据使用find的方法:

> 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" }
页: [1]
查看完整版本: mongodb的基本语法