热衷技术,热爱交流
分类: NOSQL
2013-07-14 22:52:28
MongoDB数据库基本操作
基本操作
下面列举几个常用的:
1、Help查看命令提示
db.help();
副本集信息命令提示:rs.help
2、切换/创建数据库
use test --当创建一个集合的时候会自动创建当前数据库
3、查询所有数据库
show dbs;
4、删除当前使用数据库
db.dropDatabase();
5、修复当前数据库
db.repairDatabase();
6、查看当前使用的数据库
db.getName();
7、显示当前db状态
db.stats();
8、当前db版本
db.version();
9、查看当前db的链接机器地址
db.getMongo();
Collection操作
1、创建一个聚集集合(table)
db.createCollection(“collName”);
2、得到指定名称的聚集集合(table)
db.getCollection("account");
3、得到当前db的所有聚集集合
db.getCollectionNames();
4、显示当前db所有聚集索引的状态
db.printCollectionStats();
用户相关
1、添加一个用户
db.addUser("userName", "talent123", true); --添加用户、设置密码、是否只读
2、数据库认证、安全模式
db.auth("userName", "talent123");
3、显示当前所有用户
show users;
4、删除用户
db.removeUser("userName");
集合操作
1、创建一个聚集集合(table)
db.createCollection(“collName”);
2、得到指定名称的聚集集合(table)
db.getCollection("account");
3、得到当前db的所有聚集集合
db.getCollectionNames();
4、显示当前db所有聚集索引的状态
db.printCollectionStats();
插入数据
[/usr/mdb]#mongo
MongoDB shell version: 2.4.4
connecting to: test --默认连接的是test数据库:
> show dbs --显示当前有多少数据库以及容量
local 0.078125GB
test 0.203125GB
> db --显示当前使用的数据库
test
> use ta --切换数据库(类似sqlserver和mysql语法)
switched to db ta --use命令隐式得建立了一个数据库,无需用create一类的语句建立.
> db.test.insert({id:100,name:100}) --插入命令就自动建立了集合,无需手动建立.
> db.test.find().limit(100); ----查找前100条记录
MongoDB数据插入方式多种多样,除了简单的insert方法,还有下面一些常用方法
1.save函数变量插入
> i={name:'huangxing'};
{ "name" : "huangxing" }
> j={name:'bijiben'};j={name:'bijiben'};
{ "name" : "bijiben" }
> db.test.save(i); --相当于 insert into test(name) values(‘bijiben’);
> db.test.save(j);
> db.test.find();
{ "_id" : ObjectId("51d4440c0f47849131f7e528"), "id" : 100, "name" : 100 }
{ "_id" : ObjectId("51d4455b0f47849131f7e52a"), "name" : "huangxing" }
{ "_id" : ObjectId("51d4456a0f47849131f7e52b"), "name" : "bijiben" }
2. 使用javascript语句循环插入:
> for( var i = 1; i < 10; i++ ) db.test.save( { x:i, y:12345 } );
> db.test.find();
{ "_id" : ObjectId("51d4440c0f47849131f7e528"), "id" : 100, "name" : 100 }
{ "_id" : ObjectId("51d4455b0f47849131f7e52a"), "name" : "huangxing" }
{ "_id" : ObjectId("51d4456a0f47849131f7e52b"), "name" : "bijiben" }
{ "_id" : ObjectId("51d4480c0f47849131f7e52c"), "x" : 1, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e52d"), "x" : 2, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e52e"), "x" : 3, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e52f"), "x" : 4, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e530"), "x" : 5, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e531"), "x" : 6, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e532"), "x" : 7, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e533"), "x" : 8, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e534"), "x" : 9, "y" : 12345 }
每条文档都自动生成一个类型为ObjectId的key,名字叫做_id,一个集合中每条文档的_id值必须不同,类似关系数据中的主键,但是他的类型可以是任意的,而且我们可以手动指定_id的值。
>db.test.insert({ "_id" : ObjectId("51d448680f47849131f7e534"), "x" : 8, "y" : 12345 })
E11000 duplicate key error index: ta.test.$_id_ dup key: { : ObjectId('51d448680f47849131f7e534') } --插入了重复的_id值,所以报错
>db.test.insert({ "_id" : 'ok',"x" : 8 })
> db.test.find()
………………………………………………………………………………………
{ "_id" : ObjectId("51d448680f47849131f7e53b"), "x" : 7, "y" : 12345 }
{ "_id" : ObjectId("51d448680f47849131f7e53c"), "x" : 8, "y" : 12345 }
Type "it" for more
> it --输入it查看更多行
{ "_id" : ObjectId("51d448680f47849131f7e53d"), "x" : 9, "y" : 12345 }
{ "_id" : "ok", "x" : 8 }
3. 自定义函数插入:
在用户的家目录建立.mongorc.js文件,启动mongo shell后,会自动加载这个文件,这样就可以使用它包含的函数了:
[root@RedHat-TA ~]# cat ~/.mongorc.js
function insertData(dbName, colName, num) { --此函数用于插入数据
var col = db.getSiblingDB(dbName).getCollection(colName);
for (i = 0; i < num; i++) {
col.insert({x:i}); --执行插入操作
}
print(col.count()); --打印记录条数
}
[root@RedHat-TA ~]# mongo
> insertData("test","js",3) --三个参数,分别表示数据库名、集合名和准备插入的记录条数
3
> db.js.find()
{ "_id" : ObjectId("51dfc243a7f9bc416310bb0f"), "x" : 0 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
查询
Mongodbc查询语句多种多样,现在就简单谈谈几种常用查询方法:
1. 常用方法:
> db.soc.find()
{ "_id" : ObjectId("51dfa14022b3900de099ba12"), "UUID" : 1, "RT" : ISODate("1970-01-01T00:00:02.009Z"), "CT1" : "a", "CT2" : "b", "NAME" : "c", "AREA" : "d", "SE" : "e", "NUM" : "alarm event num", "EVENT" : "event name", "LEVEL" : "event level", "RULE" : "rule", "DURA" : 4312432, "APP_P" : "app-protcol", "TRAN_P" : "tp", "A_ID" : 1, "URL" : "", "U_P" : "par", "S_IP" : "192.168.73.66", "S_PT" : 1, "D_IP" : "192.168.73.22", "D_PT" : 4342, "MSG" : "description", "EN_T" : "system type", "EN_N" : "system name", "DE_IP" : "192.168.73.33", "C_ID" : 0.7236056630499661, "T_ID" : "44", "P_ID" : "55" }
这种查询方式一行展示一条文档,文档太长时不太方便查看,
2. 使用游标风格展示展示结果:
> var c=db.soc.find() --实例化一个游标对象
> while ( c.hasNext() ) printjson( c.next() ) --调用游标对象的方法,展示结果
{
"_id" : ObjectId("51dfa14022b3900de099ba12"),
"UUID" : 1,
"RT" : ISODate("1970-01-01T00:00:02.009Z"),
"CT1" : "a",
"CT2" : "b",
"NAME" : "c",
"AREA" : "d",
"SE" : "e",
"NUM" : "alarm event num",
"EVENT" : "event name",
"LEVEL" : "event level",
"RULE" : "rule",
"DURA" : 4312432,
"APP_P" : "app-protcol",
"TRAN_P" : "tp",
"A_ID" : 1,
"URL" : "",
"U_P" : "par",
"S_IP" : "192.168.73.66",
"S_PT" : 1,
"D_IP" : "192.168.73.22",
"D_PT" : 4342,
"MSG" : "description",
"EN_T" : "system type",
"EN_N" : "system name",
"DE_IP" : "192.168.73.33",
"C_ID" : 0.7236056630499661,
"T_ID" : "44",
"P_ID" : "55"
}
sh1:SECONDARY> db.me.insert({name:100});
sh1:SECONDARY> db.me.find({"name":{$gt:1}});
{ "_id" : ObjectId("51d57b89d8efbd65d3ff1561"), "name" : 100 }
游标json格式展示结果,比较直观,看少量文档时可以使用。
3.简单条件查询
> insertData("test","testdata",10)
50
> db.testdata.find({x:1}).forEach(printjson); --采用这种方式迭代输出游标,可以不用“it”分页显示
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d941"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe5bff5f10fd078d94b"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe6bff5f10fd078d955"), "x" : 1 }
输出前4条文档:
> db.testdata.find().limit(4).forEach(printjson);
--相当于:select * from testata where rownum<=4
{ "_id" : ObjectId("51dfc243a7f9bc416310bb0f"), "x" : 0 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
从第3条记录开始,输出4行文档:
> db.testdata.find().limit(4).forEach(printjson);
{ "_id" : ObjectId("51dfc243a7f9bc416310bb0f"), "x" : 0 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
> db.testdata.find().skip(3).limit(4).forEach(printjson);
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb13"), "x" : 4 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb14"), "x" : 5 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb15"), "x" : 6 }
4. 条件操作符过滤:
<, <=, >, >=,!=
例如,要求输出的文档中,x在1-5之间
> db.testdata.find({x:{$gte:1,$lte:5}}).forEach(printjson);
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb13"), "x" : 4 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb14"), "x" : 5 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d941"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d942"), "x" : 2 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d943"), "x" : 3 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d944"), "x" : 4 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d945"), "x" : 5 }
{ "_id" : ObjectId("51dfcbe5bff5f10fd078d94b"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe5bff5f10fd078d94c"), "x" : 2 }
同理,大于是$gt,小于是$lt, 不等于是$ne
5 字段查询
> db.testdata.find({x:1},{'_id':1, 'x':1}) --select _id,x from testdata where x=1;
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
排序
--select * from testdata where x<=2 order by _id asc,x desc
> db.testdata.find({x:{$lte:2}}).sort({_id:1,x:-1})
{ "_id" : ObjectId("51e2583c2e78aa4298b4716c"), "x" : 0 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716e"), "x" : 2 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47171"), "x" : 0 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47173"), "x" : 2 }
其他常用谓词
OR
> db.testdata.find({$or:[{x:{$lte:1}}, {x:{$gte:3}}]})db.testdata.find({$or:[{x:{$lte:1}}, {x:{$gte:3}}]})
--select * from testdata where x <=1 or x >= 3
{ "_id" : ObjectId("51e2583c2e78aa4298b4716c"), "x" : 0 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716f"), "x" : 3 }
{ "_id" : ObjectId("51e2583c2e78aa4298b47170"), "x" : 4 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47171"), "x" : 0 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47174"), "x" : 3 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47175"), "x" : 4 }
IN
--select * from testdata where x in (1, 2)
> db.testdata.find({x:{$in:[1,2]}})db.testdata.find({x:{$in:[1,2]}})
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716e"), "x" : 2 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47173"), "x" : 2 }
Group by
--SELECT sum(x) FROM testdata where x!=0 GROUP BY x
>db.testdata.group({
key : {'x' : true},
cond: {'x':{$ne:0}},
reduce: function(obj,prev) { prev.sum += obj.x;},
initial: {sum : 0}
});
[
{
"x" : 1,
"sum" : 2
},
{
"x" : 2,
"sum" : 4
},
{
"x" : 3,
"sum" : 6
},
{
"x" : 4,
"sum" : 8
}
]
DISTINCT
> db.testdata.distinct('x')
[ 0, 1, 2, 3, 4 ]
更新
update() 有几个参数需要注意。
db.collection.update(criteria, objNew, upsert, mult)
criteria: 需要被更新的条件表达式
objNew: 更新表达式
upsert: 如目标记录不存在,是否插入新文档。
multi: 是否更新多个文档。
-- update testdata set x = x + 10 where x in(0,1)
> db.testdata.update({x:{$in:[0,1]}},{$inc:{x:10}},false,true)
删除
> db.testdata.remove()
//移除所有
> db.testdata.remove({x:0})
//移除x='0'的行
1、添加一个用户
db.addUser("userName", "talent123", true); --添加用户、设置密码、是否只读
2、数据库认证、安全模式
db.auth("userName", "talent123");
3、显示当前所有用户
show users;
4、删除用户
db.removeUser("userName");
集合操作
1、创建一个聚集集合(table)
db.createCollection(“collName”);
2、得到指定名称的聚集集合(table)
db.getCollection("account");
3、得到当前db的所有聚集集合
db.getCollectionNames();
4、显示当前db所有聚集索引的状态
db.printCollectionStats();
插入数据
[/usr/mdb]#mongo
MongoDB shell version: 2.4.4
connecting to: test --默认连接的是test数据库:
> show dbs --显示当前有多少数据库以及容量
local 0.078125GB
test 0.203125GB
> db --显示当前使用的数据库
test
> use ta --切换数据库(类似sqlserver和mysql语法)
switched to db ta --use命令隐式得建立了一个数据库,无需用create一类的语句建立.
> db.test.insert({id:100,name:100}) --插入命令就自动建立了集合,无需手动建立.
> db.test.find().limit(100); ----查找前100条记录
MongoDB数据插入方式多种多样,除了简单的insert方法,还有下面一些常用方法
1.save函数变量插入
> i={name:'huangxing'};
{ "name" : "huangxing" }
> j={name:'bijiben'};j={name:'bijiben'};
{ "name" : "bijiben" }
> db.test.save(i); --相当于 insert into test(name) values(‘bijiben’);
> db.test.save(j);
> db.test.find();
{ "_id" : ObjectId("51d4440c0f47849131f7e528"), "id" : 100, "name" : 100 }
{ "_id" : ObjectId("51d4455b0f47849131f7e52a"), "name" : "huangxing" }
{ "_id" : ObjectId("51d4456a0f47849131f7e52b"), "name" : "bijiben" }
2. 使用javascript语句循环插入:
> for( var i = 1; i < 10; i++ ) db.test.save( { x:i, y:12345 } );
> db.test.find();
{ "_id" : ObjectId("51d4440c0f47849131f7e528"), "id" : 100, "name" : 100 }
{ "_id" : ObjectId("51d4455b0f47849131f7e52a"), "name" : "huangxing" }
{ "_id" : ObjectId("51d4456a0f47849131f7e52b"), "name" : "bijiben" }
{ "_id" : ObjectId("51d4480c0f47849131f7e52c"), "x" : 1, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e52d"), "x" : 2, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e52e"), "x" : 3, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e52f"), "x" : 4, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e530"), "x" : 5, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e531"), "x" : 6, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e532"), "x" : 7, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e533"), "x" : 8, "y" : 12345 }
{ "_id" : ObjectId("51d4480c0f47849131f7e534"), "x" : 9, "y" : 12345 }
每条文档都自动生成一个类型为ObjectId的key,名字叫做_id,一个集合中每条文档的_id值必须不同,类似关系数据中的主键,但是他的类型可以是任意的,而且我们可以手动指定_id的值。
>db.test.insert({ "_id" : ObjectId("51d448680f47849131f7e534"), "x" : 8, "y" : 12345 })
E11000 duplicate key error index: ta.test.$_id_ dup key: { : ObjectId('51d448680f47849131f7e534') } --插入了重复的_id值,所以报错
>db.test.insert({ "_id" : 'ok',"x" : 8 })
> db.test.find()
………………………………………………………………………………………
{ "_id" : ObjectId("51d448680f47849131f7e53b"), "x" : 7, "y" : 12345 }
{ "_id" : ObjectId("51d448680f47849131f7e53c"), "x" : 8, "y" : 12345 }
Type "it" for more
> it --输入it查看更多行
{ "_id" : ObjectId("51d448680f47849131f7e53d"), "x" : 9, "y" : 12345 }
{ "_id" : "ok", "x" : 8 }
3. 自定义函数插入:
在用户的家目录建立.mongorc.js文件,启动mongo shell后,会自动加载这个文件,这样就可以使用它包含的函数了:
[root@RedHat-TA ~]# cat ~/.mongorc.js
function insertData(dbName, colName, num) { --此函数用于插入数据
var col = db.getSiblingDB(dbName).getCollection(colName);
for (i = 0; i < num; i++) {
col.insert({x:i}); --执行插入操作
}
print(col.count()); --打印记录条数
}
[root@RedHat-TA ~]# mongo
> insertData("test","js",3) --三个参数,分别表示数据库名、集合名和准备插入的记录条数
3
> db.js.find()
{ "_id" : ObjectId("51dfc243a7f9bc416310bb0f"), "x" : 0 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
查询
Mongodbc查询语句多种多样,现在就简单谈谈几种常用查询方法:
1. 常用方法:
> db.soc.find()
{ "_id" : ObjectId("51dfa14022b3900de099ba12"), "UUID" : 1, "RT" : ISODate("1970-01-01T00:00:02.009Z"), "CT1" : "a", "CT2" : "b", "NAME" : "c", "AREA" : "d", "SE" : "e", "NUM" : "alarm event num", "EVENT" : "event name", "LEVEL" : "event level", "RULE" : "rule", "DURA" : 4312432, "APP_P" : "app-protcol", "TRAN_P" : "tp", "A_ID" : 1, "URL" : "", "U_P" : "par", "S_IP" : "192.168.73.66", "S_PT" : 1, "D_IP" : "192.168.73.22", "D_PT" : 4342, "MSG" : "description", "EN_T" : "system type", "EN_N" : "system name", "DE_IP" : "192.168.73.33", "C_ID" : 0.7236056630499661, "T_ID" : "44", "P_ID" : "55" }
这种查询方式一行展示一条文档,文档太长时不太方便查看,
2. 使用游标风格展示展示结果:
> var c=db.soc.find() --实例化一个游标对象
> while ( c.hasNext() ) printjson( c.next() ) --调用游标对象的方法,展示结果
{
"_id" : ObjectId("51dfa14022b3900de099ba12"),
"UUID" : 1,
"RT" : ISODate("1970-01-01T00:00:02.009Z"),
"CT1" : "a",
"CT2" : "b",
"NAME" : "c",
"AREA" : "d",
"SE" : "e",
"NUM" : "alarm event num",
"EVENT" : "event name",
"LEVEL" : "event level",
"RULE" : "rule",
"DURA" : 4312432,
"APP_P" : "app-protcol",
"TRAN_P" : "tp",
"A_ID" : 1,
"URL" : "",
"U_P" : "par",
"S_IP" : "192.168.73.66",
"S_PT" : 1,
"D_IP" : "192.168.73.22",
"D_PT" : 4342,
"MSG" : "description",
"EN_T" : "system type",
"EN_N" : "system name",
"DE_IP" : "192.168.73.33",
"C_ID" : 0.7236056630499661,
"T_ID" : "44",
"P_ID" : "55"
}
sh1:SECONDARY> db.me.insert({name:100});
sh1:SECONDARY> db.me.find({"name":{$gt:1}});
{ "_id" : ObjectId("51d57b89d8efbd65d3ff1561"), "name" : 100 }
游标json格式展示结果,比较直观,看少量文档时可以使用。
3.简单条件查询
> insertData("test","testdata",10)
50
> db.testdata.find({x:1}).forEach(printjson); --采用这种方式迭代输出游标,可以不用“it”分页显示
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d941"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe5bff5f10fd078d94b"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe6bff5f10fd078d955"), "x" : 1 }
输出前4条文档:
> db.testdata.find().limit(4).forEach(printjson);
--相当于:select * from testata where rownum<=4
{ "_id" : ObjectId("51dfc243a7f9bc416310bb0f"), "x" : 0 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
从第3条记录开始,输出4行文档:
> db.testdata.find().limit(4).forEach(printjson);
{ "_id" : ObjectId("51dfc243a7f9bc416310bb0f"), "x" : 0 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
> db.testdata.find().skip(3).limit(4).forEach(printjson);
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb13"), "x" : 4 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb14"), "x" : 5 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb15"), "x" : 6 }
4. 条件操作符过滤:
<, <=, >, >=,!=
例如,要求输出的文档中,x在1-5之间
> db.testdata.find({x:{$gte:1,$lte:5}}).forEach(printjson);
{ "_id" : ObjectId("51dfc243a7f9bc416310bb10"), "x" : 1 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb11"), "x" : 2 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb12"), "x" : 3 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb13"), "x" : 4 }
{ "_id" : ObjectId("51dfc243a7f9bc416310bb14"), "x" : 5 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d941"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d942"), "x" : 2 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d943"), "x" : 3 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d944"), "x" : 4 }
{ "_id" : ObjectId("51dfcbe4bff5f10fd078d945"), "x" : 5 }
{ "_id" : ObjectId("51dfcbe5bff5f10fd078d94b"), "x" : 1 }
{ "_id" : ObjectId("51dfcbe5bff5f10fd078d94c"), "x" : 2 }
同理,大于是$gt,小于是$lt, 不等于是$ne
5 字段查询
> db.testdata.find({x:1},{'_id':1, 'x':1}) --select _id,x from testdata where x=1;
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
排序
--select * from testdata where x<=2 order by _id asc,x desc
> db.testdata.find({x:{$lte:2}}).sort({_id:1,x:-1})
{ "_id" : ObjectId("51e2583c2e78aa4298b4716c"), "x" : 0 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716e"), "x" : 2 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47171"), "x" : 0 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47173"), "x" : 2 }
其他常用谓词
OR
> db.testdata.find({$or:[{x:{$lte:1}}, {x:{$gte:3}}]})db.testdata.find({$or:[{x:{$lte:1}}, {x:{$gte:3}}]})
--select * from testdata where x <=1 or x >= 3
{ "_id" : ObjectId("51e2583c2e78aa4298b4716c"), "x" : 0 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716f"), "x" : 3 }
{ "_id" : ObjectId("51e2583c2e78aa4298b47170"), "x" : 4 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47171"), "x" : 0 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47174"), "x" : 3 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47175"), "x" : 4 }
IN
--select * from testdata where x in (1, 2)
> db.testdata.find({x:{$in:[1,2]}})db.testdata.find({x:{$in:[1,2]}})
{ "_id" : ObjectId("51e2583c2e78aa4298b4716d"), "x" : 1 }
{ "_id" : ObjectId("51e2583c2e78aa4298b4716e"), "x" : 2 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47172"), "x" : 1 }
{ "_id" : ObjectId("51e2583f2e78aa4298b47173"), "x" : 2 }
Group by
--SELECT sum(x) FROM testdata where x!=0 GROUP BY x
>db.testdata.group({
key : {'x' : true},
cond: {'x':{$ne:0}},
reduce: function(obj,prev) { prev.sum += obj.x;},
initial: {sum : 0}
});
[
{
"x" : 1,
"sum" : 2
},
{
"x" : 2,
"sum" : 4
},
{
"x" : 3,
"sum" : 6
},
{
"x" : 4,
"sum" : 8
}
]
DISTINCT
> db.testdata.distinct('x')
[ 0, 1, 2, 3, 4 ]
更新
update() 有几个参数需要注意。
db.collection.update(criteria, objNew, upsert, mult)
criteria: 需要被更新的条件表达式
objNew: 更新表达式
upsert: 如目标记录不存在,是否插入新文档。
multi: 是否更新多个文档。
-- update testdata set x = x + 10 where x in(0,1)
> db.testdata.update({x:{$in:[0,1]}},{$inc:{x:10}},false,true)
删除
> db.testdata.remove()
//移除所有
> db.testdata.remove({x:0})
//移除x='0'的行