Home  >  Article  >  Database  >  MongoDB简单的增、删、改、查

MongoDB简单的增、删、改、查

WBOY
WBOYOriginal
2016-06-07 16:41:15816browse

insert、remove、drop、update、find、show dbs、show tables先用一段简单的实际操作阐述下使用方法,再较详细的分析。 Linux下执行mongodb自带的mongo命令就可以进入类似mysql一样的控制界面,mongodb的数据库、集合、文档类似mysql中的数据库、表、记录的

insert、remove、drop、update、find、show dbs、show tables先用一段简单的实际操作阐述下使用方法,再较详细的分析。
Linux下执行mongodb自带的mongo命令就可以进入类似mysql一样的控制界面,mongodb的数据库、集合、文档类似mysql中的数据库、表、记录的概念,下面上干货。

#查看数据库
> show dbs;
admin   (empty)
local   0.078GB
myinfo  0.078GB

#切换数据库,如果数据库不存在,将会在增加第一条记录时自动创建该数据库
> use myinfo
switched to db myinfo

#查看集合,在向一个不存在的集合添加文档的时自动创建该集合
> show tables;
system.indexes

#定义一个文档
> doc01={'id':'10', 'name':'job', 'doc':'hello world!'}
{ "id" : "10", "name" : "job", "doc" : "hello world!" }

#查看文档
> doc01
{ "id" : "10", "name" : "job", "doc" : "hello world!" }

#将文档插入testtable集合
> db.testtable.insert(doc01)
WriteResult({ "nInserted" : 1 })

#也可以向集合直接插入文档
> db.testtable.insert({'id':'11', 'name':'jim', 'doc':'hi world!'})
WriteResult({ "nInserted" : 1 })

#再次查看集合时,会发现新创建的集合testtable
> show tables;
system.indexes
testtable

#查找集合testtable中的所有文档
> db.testtable.find()
{ "_id" : ObjectId("5476cd8e0074a24d1b6eaea7"), "id" : "10", "name" : "job", "doc" : "hello world!" }
{ "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }

#查找id为11的文档
>db.testtable.find({'id':'11'})
{ "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }

#更新id为10的文档,将name改为kiki
>  db.testtable.update({'id':'10'},{'id':'10', 'name':'kiki', 'doc':'hello workd!'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testtable.find()
{ "_id" : ObjectId("5476cd8e0074a24d1b6eaea7"), "id" : "10", "name" : "kiki", "doc" : "hello workd!" }
{ "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }

#将id等于10的文档删除
> db.testtable.remove({'id':'10'})
WriteResult({ "nRemoved" : 1 })
> db.testtable.find()
{ "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }
>

insert( )函数
使用比较简单,直接插入或间接插入已定义的文档即可。
update( )函数

     db.collection.update( criteria, objNew, upsert, multi )
     update()函数接受以下四个参数:
          criteria : update的查询条件,类似sql update查询内where后面的。
          objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
          upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
          multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

remove( )函数、dorp( )函数

     使用 remove() 函数移除数据
          如果你想移除"userdetails"集合中"user_id" 为 "testuser"的数据你可以执行以下命令:
          >db.userdetails.remove( { "user_id" : "testuser" } )
     删除所有数据
          如果你想删除"userdetails"集合中的所有数据,可以执行以下命令:
          >db.userdetails.remove({})
     使用drop()删除集合
          如果你想删除整个"userdetails"集合,包含所有文档数据,可以执行以下数据:
          >db.userdetails.drop()
     使用dropDatabase()函数删除数据库
          如果你想删除整个数据库的数据,你可以执行以下命令:
          >db.dropDatabase()

find( )函数

 从集合中获取数据
          如果你想在集合中读取所有的的数据,可以执行以下命令
          >db.userdetails.find();
          类似于如下SQL查询语句:
          Select * from userdetails;
     通过指定条件读取数据
          如果我们想在集合"userdetails"中读取"education"为"M.C.A." 的数据,我们可以执行以下命令:
          >db.userdetails.find({"education":"M.C.A."})
          类似如下SQL查询语句:
          Select * from userdetails where education="M.C.A.";
     通过条件操作符读取数据
          MongoDB中条件操作符有:
               (>) 大于 - $gt
               (=) 大于等于 - $gte
               () 大于操作符 - $gt
          如果你想获取"testtable"集合中"age" 大于22的数据,你可以使用以下命令:
          >db.testtable.find({age : {$gt : 22}})
          类似于SQL语句:
          Select * from testtable where age >22;

          MongoDB 使用 () 查询operator - $lt 和 $gt
          如果你想获取"testtable"集合中"age" 大于17以及小于24的数据,你可以执行以下命令:
          >db.testtable.find({age : {$lt :24, $gt : 17}})

     更多的查询技巧可以查看http://www.w3cschool.cc/mongodb/mongodb-query.html

文章出处:http://www.xiaomastack.com/2014/11/29/mongodb/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn