1,数据库的插入
mongod: db.pincer.insert({'a':1})
sql: insert into pincer values(1)
循环插入:
for(var i=1;i<10;i++)db.test.save('c':i);
db.test.find();
forEach() 循环输出
db.pincer.find().forEach(printjson)
2,数据的修改
mongod: db.pincer.update({'a':1},{'b':2})(结果为{'b':2})
或者通过$set(在一个文档后面继续加键值对)
db.pincer.update({'a':1},{$set:{'c':3}})
db.pincer.find()结果是 {'a':1,'c':3}
db.pincer.update({'a':1},{$set:{'a':3}})
db.pincer.find()结果是 {'a':3}
删除这个文档的c键
db.pincer.update({'a':1},{$unset:{'c':3}})
sql : update pincer set a=2 where a=1
3,删除数据
mongod: db.pincer.remove({'a':1})
sql : delete from pincer where a=1
2、查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct("name");
会过滤掉name中的相同数据
相当于:select distict name from userInfo;
4,在数组后增加值
mongod:
1,使用$addToSet
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4 ] }
> db.test.update({"_id" : ObjectId("52124f5a3806d950b31d83eb")},{"$addToSet":{'a':'aa'}}) (这里用的是id)
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa" ]
2,使用$push
> db.test.update({"a" : [ 1, 2, 3, 4, "aa" ]},{$push:{'a':'aa'}})
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa", "aa" ] } (这里用的是键值对匹配)
>
5,删除数组中的值 $pop 只能基于位置 $pull 可以基于特定条件,把匹配到的数组中的值都删掉
$pop:{key:1} 删除数组中最后一个
$pop:{key:-1} 删除数组中开头一个
查看
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa", "aa" ] }
删除key a中最后一个值
> db.test.update({"_id" : ObjectId("52124f5a3806d950b31d83eb")},{$pop:{'a':1}})
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa" ] }
> db.test.find()
{ "_id" : ObjectId("5212530a3806d950b31d83ec"), "hello" : [ 1, 2, 3, 4, 1, 1, 1 ] }
删除数组中,值为2的
> db.test.update({"_id" : ObjectId("5212530a3806d950b31d83ec")},{$pull:{'hello':2}})
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa" ] }
{ "_id" : ObjectId("5212530a3806d950b31d83ec"), "hello" : [ 1, 3, 4, 1, 1, 1 ] }
删除数组中值为1的,里面所有1都会删除
> db.test.find()
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa" ] }
{ "_id" : ObjectId("5212530a3806d950b31d83ec"), "hello" : [ 3, 4 ] }
6,条件查询
1,select * from pincer where a=1
db.pincer.find({'a':1}).forEach(printjson)
2,select j from pincer where a=1
db.pincer.find({'a':1},{j,true}).forEach(printjson)
3,select * from pincer limit 3
db.pincer.find().limit(3)
18、or与 查询
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
7, 大于,小于和等于
语法: db.collection.find({"key":{$gt/$lt/$gte/$lte:value}})
> db.pincer.find({'pincer':{$gt:1}})
{ "_id" : ObjectId("52122ce33806d950b31d83e2"), "pincer" : 3 }
{ "_id" : ObjectId("52122cdf3806d950b31d83e0"), "pincer" : 2 }
或
db.pincer.find({$where:"this.a>1"})
去大于某个值,小于某个值的
> db.test.find({'age':{$gt:1,$lt:4}})
{ "_id" : ObjectId("5212752b3806d950b31d83f7"), "age" : 2 }
{ "_id" : ObjectId("5212752c3806d950b31d83f8"), "age" : 3 }
不等于 $ne
8,$all
类似于sql 中的all
> db.test.find({'age':{$all:[1,3]}})
{ "_id" : ObjectId("521276d23806d950b31d8400"), "age" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("521276d43806d950b31d8401"), "age" : [ 1, 2, 3, 4 ] }
{ "_id" : ObjectId("521276d63806d950b31d8402"), "age" : [ 1, 2, 3, 4, 5 ] }
{ "_id" : ObjectId("521276de3806d950b31d8403"), "age" : [ 1, 3, 5, 7, 9 ] }
$in类似于sql中的in
> db.test.find({'age':{$in:[1,3]}})
{ "_id" : ObjectId("5212752a3806d950b31d83f6"), "age" : 1 }
{ "_id" : ObjectId("5212752c3806d950b31d83f8"), "age" : 3 }
{ "_id" : ObjectId("521276cf3806d950b31d83ff"), "age" : [ 1, 2 ] }
{ "_id" : ObjectId("521276d23806d950b31d8400"), "age" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("521276d43806d950b31d8401"), "age" : [ 1, 2, 3, 4 ] }
{ "_id" : ObjectId("521276d63806d950b31d8402"), "age" : [ 1, 2, 3, 4, 5 ] }
{ "_id" : ObjectId("521276de3806d950b31d8403"), "age" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("521277133806d950b31d8404"), "age" : [ 1, 4, 5 ] }
$nin
> db.test.find({'age':{$nin:[1,3]}})
9,判断某个字段是否存在
db.collection.find({'key':{$exists:true}})
> db.pincer.find({'pincer':{$exists:true}})
{ "_id" : ObjectId("52122a923806d950b31d83df"), "pincer" : "sarah" }
{ "_id" : ObjectId("52122ce33806d950b31d83e2"), "pincer" : 3 }
{ "_id" : ObjectId("52122cdf3806d950b31d83e0"), "pincer" : 2 }
> db.pincer.find({'pincer':{$exists:false}})
{ "_id" : ObjectId("52122ce83806d950b31d83e4"), "sarah" : 2 }
{ "_id" : ObjectId("52122ce93806d950b31d83e5"), "sarah" : 4 }
{ "_id" : ObjectId("52122e263806d950b31d83e7"), "a" : 0 }
{ "_id" : ObjectId("5212361f3806d950b31d83e8"), "b" : 2, "c" : 3, "d" : { "q" : 1, "w" : 2 } }
{ "_id" : ObjectId("52122e203806d950b31d83e6"), "b" : 0, "d" : { "q" : 1, "w" : 2 } }
{ "_id" : ObjectId("521244e03806d950b31d83ea"), "a" : [ "aaa", "bbbb", "cccc", "dddd" ] }
{ "_id" : ObjectId("52122ce63806d950b31d83e3"), "sarah" : "3_1", "ss" : 111 }
>
10,null值的处理
查询某个key值为null的,会返回值为null且 ,对应没有这个key的记录
11,$mod 去模运算 下面是
> db.test.find({'age':{$mod:[4,1]}})
{ "_id" : ObjectId("5212752a3806d950b31d83f6"), "age" : 1 }
{ "_id" : ObjectId("5212752f3806d950b31d83fa"), "age" : 5 }
{ "_id" : ObjectId("521275353806d950b31d83fe"), "age" : 9 }
可以看到|4-5|=1,||
12,查询记录的总条数
> db.test.find().count()
22
> db.test.find().count()
22
> db.test.find().skip(2).count(true)
20
> db.test.find().skip(2).limit(10).count(true)
10
13 排序
db.test.find().sort({'age':1}) 1表示升序
db.test.find().sort({'age':-1}) -1表示降序
count() 统计记录数
db.pincer.count()
db.pincer.skip(3).count()
db.pincer.find({"age":1}).count()
distinct 去除重复
db.runCommand("distinct":collections,"key":keyword)
distinct 是关键字,collections是对查询的集合
key是关键字 , keyword是要查询的集合中的键
如: 有个pincer集合,里面有age:1 等键值对
> db.runCommand({"distinct":"pincer","key":"age"})
{
"values" : [
null,
1,
2
],
"stats" : {
"n" : 15,
"nscanned" : 15,
"nscannedObjects" : 0,
"timems" : 0,
"cursor" : "BtreeCursor age_1"
},
"ok" : 1
}
14查看当前使用的数据库
db.getName();
15,删除当前数据库
db.dropDatabase()
-----索引
1、创建索引
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});
2、查询当前聚集集合所有索引
db.userInfo.getIndexes();
3、查看总索引记录大小
db.userInfo.totalIndexSize();
4、读取当前集合的所有index信息
db.users.reIndex();
5、删除指定索引
db.users.dropIndex("name_1");
9、显示当前db状态
db.stats();
10、当前db版本
db.version();
11、查看当前db的链接机器地址
db.getMongo();
6、删除所有索引索引
db.users.dropIndexes();
\
7,查询age 的时候,使用explain可以查看是否使用索引
> db.pincer.find({"age":1}).explain()
{
"cursor" : "BtreeCursor age_1", 使用了索引age_1
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 2,
"nscanned" : 2,
"nscannedObjectsAllPlans" : 2,
"nscannedAllPlans" : 2,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"age" : [
[
1,
1
]
]
},
"server" : "localhost.localdomain:27017"
---------------------------------------------------------------
正则表达式
1,$not 查询name中不以a开头的
> db.test.find({'name':{$not:/^a/}})
{ "_id" : ObjectId("52124f5a3806d950b31d83eb"), "a" : [ 1, 2, 3, 4, "aa" ] }
{ "_id" : ObjectId("5212530a3806d950b31d83ec"), "hello" : [ 3, 4 ] }
{ "_id" : ObjectId("5212752a3806d950b31d83f6"), "age" : 1 }
{ "_id" : ObjectId("5212752b3806d950b31d83f7"), "age" : 2 }
{ "_id" : ObjectId("5212752c3806d950b31d83f8"), "age" : 3 }
{ "_id" : ObjectId("5212752e3806d950b31d83f9"), "age" : 4 }
{ "_id" : ObjectId("5212752f3806d950b31d83fa"), "age" : 5 }
{ "_id" : ObjectId("521275313806d950b31d83fb"), "age" : 6 }
{ "_id" : ObjectId("521275323806d950b31d83fc"), "age" : 7 }
{ "_id" : ObjectId("521275343806d950b31d83fd"), "age" : 8 }
{ "_id" : ObjectId("521275353806d950b31d83fe"), "age" : 9 }
{ "_id" : ObjectId("521276cf3806d950b31d83ff"), "age" : [ 1, 2 ] }
{ "_id" : ObjectId("521276d23806d950b31d8400"), "age" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("521276d43806d950b31d8401"), "age" : [ 1, 2, 3, 4 ] }
{ "_id" : ObjectId("521276d63806d950b31d8402"), "age" : [ 1, 2, 3, 4, 5 ] }
{ "_id" : ObjectId("521276de3806d950b31d8403"), "age" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("521277133806d950b31d8404"), "age" : [ 1, 4, 5 ] }
{ "_id" : ObjectId("521277f83806d950b31d8406"), "name" : "bbbb" }
{ "_id" : ObjectId("521277fa3806d950b31d8407"), "name" : "cccc" }
{ "_id" : ObjectId("521278003806d950b31d8409"), "name" : "desc" }
2,游标
> for(var c=db.test.find();c.hasNext();){printjson(c.next())}
{
"_id" : ObjectId("52124f5a3806d950b31d83eb"),
"a" : [
1,
2,
3,
4,
"aa"
]
}
或
使用forEach
db.pincer.find().forEach(printjson)
3,存储过程
> function addnum(x,y){
... return x+y
... }
将sql执行以函数转换为mongdb的存储过程
> db.system.js.save({_id:"addnum",value:function(x,y){return x+y}})
调用存储过程
> db.eval('addnum(1,5)')
1、简单Hello World
print("Hello World!");
这种写法调用了print函数,和直接写入"Hello World!"的效果是一样的;
2、将一个对象转换成json
tojson(new Object());
tojson(new Object('a'));
3、循环添加数据
> for (var i = 0; i < 30; i++) {
... db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
... };
这样就循环添加了30条数据,同样也可以省略括号的写法
> for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
也是可以的,当你用db.users.find()查询的时候,显示多条数据而无法一页显示的情况下,可以用it查看下一页的信息;
4、find 游标查询
>var cursor = db.users.find();
> while (cursor.hasNext()) {
printjson(cursor.next());
}
这样就查询所有的users信息,同样可以这样写
var cursor = db.users.find();
while (cursor.hasNext()) { printjson(cursor.next); }
同样可以省略{}号
5、forEach迭代循环
db.users.find().forEach(printjson);
forEach中必须传递一个函数来处理每条迭代的数据信息
6、将find游标当数组处理
var cursor = db.users.find();
cursor[4];
取得下标索引为4的那条数据
既然可以当做数组处理,那么就可以获得它的长度:cursor.length();或者cursor.count();
那样我们也可以用循环显示数据
for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);
7、将find游标转换成数组
> var arr = db.users.find().toArray();
> printjson(arr[2]);
用toArray方法将其转换为数组
8、定制我们自己的查询结果
只显示age <= 28的并且只显示age这列数据
db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);
db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);
排除age的列
db.users.find({age: {$lte: 28}}, {age: false}).forEach(printjson);
9、forEach传递函数显示信息
db.things.find({x:4}).forEach(function(x) {print(tojson(x));});
上面介绍过forEach需要传递一个函数,函数会接受一个参数,就是当前循环的对象,然后在函数体重处理传入的参数信息。
10,查看当前所有集合
show dbs;
查看某个集合的信息
db.pincer.stats()