MongoDB基本操作

下载

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.6.tgz

tar -zxvf http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.6.tgz -C /usr/local/ && mv /usr/local/mongodb-linux-x86_64-2.4.6 /usr/local/mongodb

export PATH=$PATH:/usr/local/mongodb/bin && echo "export PATH=$PATH:/usr/local/mongodb/bin" >> /etc/profile


单机版启动

mongod --fork --bind_ip 127.0.0.1 --port 11811 --dbpath /opt/mongodata --directoryperdb --logpath /var/log/mongodb.log --logappend --smallfiles


基本操作

[root@master local]# mongo localhost:11811               //连接到server
MongoDB shell version: 2.4.6
connecting to: localhost:11811/test
>

> db                   //查看当前的db
test

> show dbs;       //查看所有的db
local    0.03125GB
test    (empty)


> use test;      //进入数据库test
switched to db test

> j = {name:"cai_test"}        //创建一个j文件的js
{ "name" : "cai_test" }
> k = {name: "wang_test"} //创建一个k文件的js

{ "name" : "wang_test" }

> db.testData.insert(j)     //将j插入testData
> db.testData.insert(k)    //将k插入testData
> show collections          //查看数据集是否存在
system.indexes
testData


> db.testData.find();                 //查询数据集testData的内容
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }                //这个ID值是自动加上去的,就像oracle中的rowid一样,但是没有隐藏而已
{ "_id" : ObjectId("522bb3a0674205792fd82cc8"), "name" : "wang_test" }

插入文件可以使用javascript或者for循环

> var c = db.testData.find()                     //定义一个变量c,c的内容是testData的查询结果集

> while ( c.hasNext()) printjson( c.next())           //然后用while循环打印处c变量的结果

{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }
{ "_id" : ObjectId("522bb3a0674205792fd82cc8"), "name" : "wang_test" }


> var c = db.testData.find()                //定义c变量
> printjson( c[1] )
{ "_id" : ObjectId("522bb3a0674205792fd82cc8"), "name" : "wang_test" }                //打印处查询C变量中结果集的第二条数据
> printjson( c[0] )
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }                     //打印C结果集中的第一条数据


查询特定文件

> db.testData.find( { "name" : "cai_test" } )             //查询name等于cai_test的文件,相当与关系型数据库中的select * from testData where name=cai_test;语句
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }

> db.testData.findOne()                                      //这个查询和上述find查询一样 只是查询结果从集合返回一个单一文件而已
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }
> db.testData.findOne( { "name" : "wang_test" } )
{ "_id" : ObjectId("522bb3a0674205792fd82cc8"), "name" : "wang_test" }


限制结果集的条数

> db.testData.find().limit(2)                       //此处我的数据只有两条,结果如下
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }
{ "_id" : ObjectId("522bb3a0674205792fd82cc8"), "name" : "wang_test" }
> db.testData.find().limit(1)
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }
> db.testData.find().limit(0)
{ "_id" : ObjectId("522bb394674205792fd82cc7"), "name" : "cai_test" }
{ "_id" : ObjectId("522bb3a0674205792fd82cc8"), "name" : "wang_test" }


快速生成测试数据

> for (var i = 1; i <= 20; i++) db.john.insert( { "id" : i} )                 //c程序语言风格的for循环 插入10条数据

> db.john.find()
{ "_id" : ObjectId("522bd627768503f0c954e5cd"), "id" : 1 }
{ "_id" : ObjectId("522bd627768503f0c954e5ce"), "id" : 2 }
{ "_id" : ObjectId("522bd627768503f0c954e5cf"), "id" : 3 }
{ "_id" : ObjectId("522bd627768503f0c954e5d0"), "id" : 4 }
{ "_id" : ObjectId("522bd627768503f0c954e5d1"), "id" : 5 }
{ "_id" : ObjectId("522bd627768503f0c954e5d2"), "id" : 6 }
{ "_id" : ObjectId("522bd627768503f0c954e5d3"), "id" : 7 }
{ "_id" : ObjectId("522bd627768503f0c954e5d4"), "id" : 8 }
{ "_id" : ObjectId("522bd627768503f0c954e5d5"), "id" : 9 }
{ "_id" : ObjectId("522bd627768503f0c954e5d6"), "id" : 10 }
{ "_id" : ObjectId("522bd627768503f0c954e5d7"), "id" : 11 }
{ "_id" : ObjectId("522bd627768503f0c954e5d8"), "id" : 12 }
{ "_id" : ObjectId("522bd627768503f0c954e5d9"), "id" : 13 }
{ "_id" : ObjectId("522bd627768503f0c954e5da"), "id" : 14 }
{ "_id" : ObjectId("522bd627768503f0c954e5db"), "id" : 15 }
{ "_id" : ObjectId("522bd627768503f0c954e5dc"), "id" : 16 }
{ "_id" : ObjectId("522bd627768503f0c954e5dd"), "id" : 17 }
{ "_id" : ObjectId("522bd627768503f0c954e5de"), "id" : 18 }
{ "_id" : ObjectId("522bd627768503f0c954e5df"), "id" : 19 }
{ "_id" : ObjectId("522bd627768503f0c954e5e0"), "id" : 20 }


定义一个方法插入数据(类似与C语言中的自定义函数)

> function insertData(dbName, colName, num) {                 //定义一个方法名字交insertData
... var col = db.getSiblingDB(dbName).getCollection(colName);
... for (i = 0; i < num; i++) {
... col.insert({x:i});
... }
... print(col.count());
... }
> insertData( "test" , "john1", 20 )                               //引用insertData函数 插入到数据库test中的john1文件集中20条数据
20
> show collections;      //它已经新建了一个文件集合john1
caitest
john
john1                      //this
system.indexes
testData
> db.john1.find();                  //查看john1中的数值
{ "_id" : ObjectId("522bd8b6768503f0c954e5e1"), "x" : 0 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e2"), "x" : 1 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e3"), "x" : 2 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e4"), "x" : 3 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e5"), "x" : 4 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e6"), "x" : 5 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e7"), "x" : 6 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e8"), "x" : 7 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5e9"), "x" : 8 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5ea"), "x" : 9 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5eb"), "x" : 10 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5ec"), "x" : 11 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5ed"), "x" : 12 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5ee"), "x" : 13 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5ef"), "x" : 14 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5f0"), "x" : 15 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5f1"), "x" : 16 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5f2"), "x" : 17 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5f3"), "x" : 18 }
{ "_id" : ObjectId("522bd8b6768503f0c954e5f4"), "x" : 19 }


#######################此部分摘自网络#######################

这里主要是讲MongoDB在控制台中如何进行高级查询,既有教程内容,也有ME动手实验的经验,搞懂了这些规则,对于你再使用其他语言(Java,ruby,python等)实现查询时有莫大的帮助,因为基础的是相通的,只是不同的语言实现接口略有差异而已。

还有一句想提醒大家,多动手实验,才是硬道理。

 

 

 

<,>,>=,<=


这四个就不用解释了,最常用的,也是最简单的。

 

db.collection.find({ "field" : { $gt: value } } )   // 大于  : field > value

db.collection.find({ "field" : { $lt: value } } )   // 小于  :  field < value

db.collection.find({ "field" : { $gte: value } } )  // 大于等于 : field >= value

db.collection.find({ "field" : { $lte: value } } )  // 小于等于 : field <= value

 

如果要同时满足多个条件,记得要这样用:

 

db.collection.find({ "field" : { $gt: value1, $lt: value2 } } )    // value1 < field < value

 

 

 

 

$ne   不等于

 

db.things.find( { x : { $ne : 3 } } )

 

条件相当于x<>3,即x不等于3。

 

 

 

 

 

$mod    取模运算

 

db.things.find( { a : { $mod : [ 10 , 1 ] } } )

 

条件相当于a % 10 == 1 即a除以10余数为1的。





$nin  不属于

 

db.things.find({j:{$nin: [2,4,6]}})

 

条件相当于 j 不等于 [2,4,6] 中的任何一个。





$in     属于

 

db.things.find({j:{$in: [2,4,6]}})

 

条件相当于j等于[2,4,6]中的任何一个。





$all  全部属于

 

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

 

与$in类似,但必须是[]的值全部都存在。





$size     数量,尺寸

 

db.things.find( { a : { $size: 1 } } )

 

条件相当于a的值的数量是1(a必须是数组,一个值的情况不能算是数量为1的数组)。





$exists   字段存在

 

db.things.find( { a : { $exists : true } } )

db.things.find( { a : { $exists : false } } )

 

true返回存在字段a的数据,false返回不存在字度a的数据。





$type     字段类型

 

db.things.find( { a : { $type : 2 } } )

 

条件是a类型符合的话返回数据。


参数类型如下图:


Type Name

Type Number

Double

1

String

2

Object

3

Array

4

Binary data

5

Object id

7

Boolean

8

Date

9

Null

10

Regular expression

11

JavaScript code

13

Symbol

14

JavaScript code with scope

15

32-bit integer

16

Timestamp

17

64-bit integer

18

Min key

255

Max key

127

 

 

 

 

Regular Expressions    正则表达式

 

db.customers.find( { name : /acme.*corp/i } )

 

类似sql中的like方法。


行开始 /^ 行结束 $/


这里要特别特别特别地注意一点,关乎查询效率:

 

 

While /^a/, /^a./, and /^a.$/ are equivalent and will all use an index in the same way, the later two require scanning the whole string so they will be slower. The first format can stop scanning after the prefix is matched.

 

 

意思大概就是指在查询以a开头字符串时,可以有三种形式, /^a/, /^a./,和/^a.$/ 。后面两种形式会扫描整个字符串,查询速度会变慢。第一种形式会在查到符合的开头后停止扫描后面的字符。


所以要特别注意。


几个附加参数:


i的意思是忽略大小写。(这个很重要,很常用)


m的意思是支持多行。(不过ME没有尝试过)


x的意思是扩展。(也没用过)

 

 

 

 

$or  或 (注意:MongoDB 1.5.3后版本可用)

 

db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )

 

符合条件a=1的或者符合条件b=2的数据都会查询出来。


与其他字段一起查询:

 

db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

 

符合条件name等于bob,同时符合其他两个条件中任意一个的数据。

 

 

 

 

 

 

Value in an Array   数组中的值


例如数据库中存在这样的数据:

 

{ "_id" : ObjectId("4c503405645fa23b31e11631"), "colors" : [ "red", "black" ] }

 

查询

 

db.things.find( { colors : "red" } );

 

即可查到上面那条数据。





$elemMatch   要素符合

 

t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } ) 

 

结果:

 

{ "_id" : ObjectId("4b5783300334000000000aa9"),

  "x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]

}

 

x其中一个要素符合那个检索条件就可以被检索出来。(不过一般谁用像x这样的结构去保存数据呢?)

 

 

 

 

 

Value in an Embedded Object    内嵌对象中的值


例如数据库中存在这样的数据:

 

{ "_id" : ObjectId("4c503773645fa23b31e11632"), "author" : { "name" : "Dan Brown", "age" : 38 }, "book" : "The Lost Symbol" }

 

查询:

 

db.postings.find( { "author.name" : "Dan Brown" } );

 

即可查到上面那条数据。


查询内嵌对象的属性,记得要加上“”,字段是“author.name”,而不是author.name。

 

 

 

 

 

$not 不是

 

db.customers.find( { name : { $not : /acme.*corp/i } } );

 

这是一个与其他查询条件组合使用的操作符,不会单独使用。


只要你理解了前面的查询操作即可,只是再加上了$not,结果就是得到了没有$not的相反结果集。

sort()    排序


这个非常实用。即sql语言中的OrderBy

 

db.myCollection.find().sort( { ts : -1 } )

 

也可以多个字段排序

 

db.myCollection.find().sort( { ts : -1 ,ds : 1 } )

 

这里的1代表升序,-1代表降序。


经过ME的实验,小于0的数字就是降序,0以上(包括0)就是升序。

 

 

 

 

 

 

limit()   skip()

 

这两个ME想连起来讲,他们就是你实现数据库分页的好帮手

 

limit()控制返回结果数量,如果参数是0,则当作没有约束,limit()将不起作用。

 

skip()控制返回结果跳过多少数量,如果参数是0,则当作没有约束,skip()将不起作用,或者说跳过了0条。

 

例如:

 

 db.test.find().skip(5).limit(5)

 

结果就是取第6条到第10条数据。

 

 

 

 

 

 

 

 

snapshot()   (没有尝试)

 

 

 

 

 

 

count()   条数

 

返回结果集的条数。

 

db.test.count()

 

在加入skip()limit()这两个操作时,要获得实际返回的结果数,需要一个参数true,否则返回的是符合查询条件的结果总数。

 

例子如下:

 

> db.test.find().skip(5).limit(5).count()

9

> db.test.find().skip(5).limit(5).count(true)

#######################此部分摘自网络##########################


##########################

迷途小运维学习MongoDB笔记

作者:john

转载请注明出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值