论坛首页 Java企业应用论坛

深入浅出MongoDB(四):常用命令详细讲解

浏览 2828 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-08-26  

创建数据库,集合

由于MongoDB不是关系型数据库文件,实际上,它并不存在传统关系型数据库中所谓数据库的概念,当你第一次新增数据时,MongoDB就会以collection集合的开式进行保存和新建,而不需要你提前去建立

列出所有/当前的数据库

使用show dbs命令查看所有的数据库db命令查询当前链接的数据库

huangyineng@ubuntu:/usr/local/mongodb/bin$ ./mongo

MongoDB shell version: 2.0.7

connecting to: test

> show dbs

local   (empty)

> db

test

有些同学可能感觉到很奇怪,为什么连接了test数据库,在显示所有数据库的时候竟没有test呢?这是因为连接test时,并没有向其中写数据,所以并没有真正的建立、只是在使用当前的数据库。

定义新的数据库连接

用“use 数据库名”来定义新的数据库连接,和上面的test一样,只是建立了连接

> use mydb

switched to db mydb

> show dbs

local   (empty)

> db

mydb

增加文档

MongoDB插入原理:使用驱动程序进行插入的时候,会装数据转换成BSON格式,数据库会解析BSON,并检查是否含有“_id”键,因为“_id”键在插入到数据库时MongoDB会自动生成。

定义一个collection(集合),名为users,然后插入文档:

> db.users.insert({name:"mongodb"});

> show dbs

local   (empty)

mydb    0.0625GB

> show collections

system.indexes

users

> show tables

system.indexes

users

> db.users.find()

{ "_id" : ObjectId("503253a308186c0b9de9ae8f"), "name" : "mongodb" }

> db.users.save({name:"naxsu"})

> db.users.find()

{ "_id" : ObjectId("503253a308186c0b9de9ae8f"), "name" : "mongodb" }

{ "_id" : ObjectId("5032571608186c0b9de9ae90"), "name" : "nosql" }

{ "_id" : ObjectId("5032577308186c0b9de9ae91"), "name" : "naxsu" }

首先用insert(用save也可以)语句插入一条数据,使用的是users集合,插入成功后查看所有的库,多了一个mydb库,再查看所有的集合(show collectionsshow tables),多了一个users集合,再用db.users.find()查看users集合中的所有文档。

可以增加字段

> db.users.insert({name:'name1',sex:'man'})

> db.users.find()

{ "_id" : ObjectId("503253a308186c0b9de9ae8f"), "name" : "mongodb" }

{ "_id" : ObjectId("5032571608186c0b9de9ae90"), "name" : "nosql" }

{ "_id" : ObjectId("5032577308186c0b9de9ae91"), "name" : "naxsu" }

{ "_id" : ObjectId("503259c895e66fd3daf3beaa"), "name" : "name1", "sex" : "man" }

id自增长的解决方案:定义一个函数,在插入_id时调用自己定义的函数来实现id自增

function counter(name){

var ret = db.counters.findAndModify({query:{_id:name},update:{$inc:{next:1}},”new”:true,upsert:true});

return ret.next;

}

> db.users.insert({_id:counter(“users”),name:”id1”})//_id:1

> db.users.insert({_id:counter(“users”),name:”id2”})//_id:2

注:system.indexes是由所有文档的_id组成的索引集合。

存储在MongoDB集合中的每个文档都有一个默认的主键_id,它必须是唯一的,这个主键名称是固定的,它可以是MongoDB支持的任何数据类型,默认是ObjectId.

Insertsave的不同点:如果已存在_idInsert会插入失败,而save会更新原有的数据

删除文档

删除文档用remove命令

语法格式:db.collname.remove({条件}),不写条件删除所有记录

> db.users.remove({name:"naxsu"})

> db.users.find()

{ "_id" : ObjectId("503253a308186c0b9de9ae8f"), "name" : "mongodb" }

{ "_id" : ObjectId("5032571608186c0b9de9ae90"), "name" : "nosql" }

{ "_id" : ObjectId("503259c895e66fd3daf3beaa"), "name" : "name1", "sex" : "man" }

> db.users.remove()

> db.users.find()

更新文档

先来创建两条记录,然后进行更新操作

> db.users.insert({name:"mongodb"})

> db.users.insert({name:"nosql",sex:"none"})

> db.users.find()

{ "_id" : ObjectId("50325c0a95e66fd3daf3beab"), "name" : "mongodb" }

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "nosql", "sex" : "none" }

> db.users.update({name:"mongodb"},{name:"mongodbname"})

> db.users.find()

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "nosql", "sex" : "none" }

{ "_id" : ObjectId("50325c0a95e66fd3daf3beab"), "name" : "mongodbname" }

我们用上面这种方法来更新另外一条记录

> db.users.update({name:"nosql"},{name:"mynosql"})

> db.users.find()

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "mynosql" }

{ "_id" : ObjectId("50325c0a95e66fd3daf3beab"), "name" : "mongodbname" }

发现了没,sex字段竟没有了,这时候set关键字就派上用场了

> db.users.insert({name:"nosql",sex:"man"})

> db.users.find()

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "mynosql" }

{ "_id" : ObjectId("50325c0a95e66fd3daf3beab"), "name" : "mongodbname" }

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosql", "sex" : "man" }

> db.users.update({name:"nosql"},{$set:{name:"nosqls"}})

> db.users.find()

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "mynosql" }

{ "_id" : ObjectId("50325c0a95e66fd3daf3beab"), "name" : "mongodbname" }

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosqls", "sex" : "man" }

 

查询文档

查询用findfindOne(查询一条)

语法格式:db.collname.find({条件}),不写条件查询所有的数据

> db.users.find()

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "mynosql" }

{ "_id" : ObjectId("50325c0a95e66fd3daf3beab"), "name" : "mongodbname" }

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosqls", "sex" : "man" }

> db.users.find ({name:"nosqls"})

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosqls", "sex" : "man" }

> db.users.findOne()

{ "_id" : ObjectId("50325c3595e66fd3daf3beac"), "name" : "mynosql" }

查询指定的字段,_id默认是显示的,如果不要其显示,用_id:0

> db.users.find({name:'nosqls'})

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosqls", "sex" : "man" }

> db.users.find({name:"nosqls"},{name:1})

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosqls" }

> db.users.find({name:"nosqls"},{name:1,sex:1})

{ "_id" : ObjectId("50325e87b8e32f5aeae17d8c"), "name" : "nosqls", "sex" : "man" }

> db.users.find({name:"nosqls"},{name:1,sex:1,_id:0})

{ "name" : "nosqls", "sex" : "man" }

mysql中,我们有这样的语句:

Select * from t1 order by age desc limit 1,2

Select count(*) from t1

这在MongoDB中是否也有类似的函数呢?答案是肯定的,下面我们来插入一些数据来操作

> for(i=1;i<=10;i++){

... db.c1.insert({name:"user"+i,age:i});

... }

> db.c1.find()

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

{ "_id" : ObjectId("5032d0b73142123b5a215d52"), "name" : "user3", "age" : 3 }

{ "_id" : ObjectId("5032d0b73142123b5a215d53"), "name" : "user4", "age" : 4 }

{ "_id" : ObjectId("5032d0b73142123b5a215d54"), "name" : "user5", "age" : 5 }

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d59"), "name" : "user10", "age" : 10 }

count()统计文档数

sort来对文档进行排序,age:-1表示以age降序来显示,age:1表示以age升序来显示

limit来限制输出几条文档,用skip跳过几个再输出,limitskipsortcount的可以组合使用

> db.c1.count()

10

> db.c1.find().sort({age:-1})

{ "_id" : ObjectId("5032d0b73142123b5a215d59"), "name" : "user10", "age" : 10 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d54"), "name" : "user5", "age" : 5 }

{ "_id" : ObjectId("5032d0b73142123b5a215d53"), "name" : "user4", "age" : 4 }

{ "_id" : ObjectId("5032d0b73142123b5a215d52"), "name" : "user3", "age" : 3 }

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

> db.c1.find().sort({age:1})

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

{ "_id" : ObjectId("5032d0b73142123b5a215d52"), "name" : "user3", "age" : 3 }

{ "_id" : ObjectId("5032d0b73142123b5a215d53"), "name" : "user4", "age" : 4 }

{ "_id" : ObjectId("5032d0b73142123b5a215d54"), "name" : "user5", "age" : 5 }

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d59"), "name" : "user10", "age" : 10 }

> db.c1.find().limit(2);

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

> db.c1.find().sort({name:-1}).limit(2);

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

> db.c1.find().skip(5).limit(3);

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

> db.c1.find().skip(5).limit(3).count();

10

> db.c1.find().skip(5).limit(3).count(1)

3

> db.c1.find().skip(5).limit(3).count(0)

10

注:

如果limitskipsortcount组合使用,是先排序、再跳过几条,然后再限制输出几条。

count如果不输入参数或者输入为0,会把find()后面的参数忽略,如果他的参数不为0,则统计在前面的条件过滤后的结果

条件表达式

1.         <($lt),<=($lte),>($gt),>=($gte) ,!=($ne)

> db.c1.find({age:{$gt:5}})

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d59"), "name" : "user10", "age" : 10 }

> db.c1.find({age:{$gt:5,$lte:8}})

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

2.         $all

$all操作要求数组里面的值全部被包含在返回的记录里面,如:

> db.c2.insert({a:[123,1,2,3,4]})

> db.c2.find()

{ "_id" : ObjectId("5032d6a63142123b5a215d5a"), "a" : [ 123, 1, 2, 3, 4 ] }

> db.c2.find({a:{$all:[2,3]}})

{ "_id" : ObjectId("5032d6a63142123b5a215d5a"), "a" : [ 123, 1, 2, 3, 4 ] }

> db.c2.find({a:{$all:[2,6]}})

3.         $exists

$exists操作检查一个字段是否存在,如:

> db.c2.find({age:{$exists:1}})

> db.c2.find({a:{$exists:1}})

{ "_id" : ObjectId("5032d6a63142123b5a215d5a"), "a" : [ 123, 1, 2, 3, 4 ] }

4.         $mod

$mod操作可以让我们简单的进行取模操作,而不需要用到where子句,如:

> db.c1.find({age:{$mod:[2,1]}})

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

{ "_id" : ObjectId("5032d0b73142123b5a215d52"), "name" : "user3", "age" : 3 }

{ "_id" : ObjectId("5032d0b73142123b5a215d54"), "name" : "user5", "age" : 5 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

5.         $in, $nin

$in操作类似于传统关系数据库中的IN, $nin$in操作相反

> db.c1.find({age:{$in:[4,5,6]}})

{ "_id" : ObjectId("5032d0b73142123b5a215d53"), "name" : "user4", "age" : 4 }

{ "_id" : ObjectId("5032d0b73142123b5a215d54"), "name" : "user5", "age" : 5 }

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

> db.c1.find({age:{$nin:[4,5,6]}})

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

{ "_id" : ObjectId("5032d0b73142123b5a215d52"), "name" : "user3", "age" : 3 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d59"), "name" : "user10", "age" : 10 }

6.         $nor$or

> db.c1.find({$or:[{name:"user2"},{name:"user3"}]})

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

{ "_id" : ObjectId("5032d0b73142123b5a215d52"), "name" : "user3", "age" : 3 }

> db.c1.find({$nor:[{name:"user2"},{name:"user3"},{name:"user4"},{name:"user5"}]})

{ "_id" : ObjectId("5032d0b73142123b5a215d50"), "name" : "user1", "age" : 1 }

{ "_id" : ObjectId("5032d0b73142123b5a215d55"), "name" : "user6", "age" : 6 }

{ "_id" : ObjectId("5032d0b73142123b5a215d56"), "name" : "user7", "age" : 7 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

{ "_id" : ObjectId("5032d0b73142123b5a215d58"), "name" : "user9", "age" : 9 }

{ "_id" : ObjectId("5032d0b73142123b5a215d59"), "name" : "user10", "age" : 10 }

> db.c1.find({$or:[{name:"user2"},{age:8}]})

{ "_id" : ObjectId("5032d0b73142123b5a215d51"), "name" : "user2", "age" : 2 }

{ "_id" : ObjectId("5032d0b73142123b5a215d57"), "name" : "user8", "age" : 8 }

7.         $size

$size操作将会查询数组长度等于输入参数的数组,例子:

> db.c2.find();

{ "_id" : ObjectId("5032d6a63142123b5a215d5a"), "a" : [ 123, 1, 2, 3, 4 ] }

{ "_id" : ObjectId("50337a8aed4213674aebade4"), "b" : [ 568, 89, 52 ] }

> db.c2.find({a:{$size:4}})

> db.c2.find({a:{$size:5}})

{ "_id" : ObjectId("5032d6a63142123b5a215d5a"), "a" : [ 123, 1, 2, 3, 4 ] }

 本文链接:http://www.656463.com/portal.php?mod=view&aid=78,转载请注明出处

下一节:深入浅出MongoDB(五):使用Java操作MongoDB

 

 

 

   发表时间:2012-08-27  
下一节:深入浅出MongoDB(五):使用Java操作MongoDB
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics