MongoDB使用总结


一. 超级用户相关:
1. # 进入数据库 admin
use admin
2. # 增加或修改用户密码
db.addUser('name','pwd')
3. # 查看用户列表
db.system.users.find()
4. # 用户认证
db.auth('name','pwd')
5. # 删除用户
db.removeUser('name')
6. # 查看所有用户
show users
7. # 查看所有数据库
show dbs
8. # 查看所有的 collection
show collections
9. # 查看各 collection 的状态
db.printCollectionStats()
10. # 查看主从复制状态
db.printReplicationInfo()
11. # 修复数据库
db.repairDatabase()
12. # 设置记录 profiling , 0=off 1=slow 2=all
db.setProfilingLevel(1)
13. # 查看 profiling
show profile
14. # 拷贝数据库
db.copyDatabase('mail_addr','mail_addr_tmp')
15. # 删除 collection
db.mail_addr.drop()
16. # 删除当前的数据库
db.dropDatabase()
二 . 增删改
1. # 存储嵌套的对象
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
2. # 存储数组对象
db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
3. # 根据 query 条件修改,如果不存在则插入,允许修改多条记录
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
4. # 删除 yy=5 的记录
db.foo.remove({'yy':5})
5. # 删除所有的记录
db.foo.remove()
三. 索引

1. # 增加索引: 1(ascending),-1(descending)
2. db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
3. # 索引子对象
4. db.user_addr.ensureIndex({'Al.Em': 1})
5. # 查看索引信息
6. db.foo.getIndexes()
7. db.foo.getIndexKeys()
8. # 根据索引名删除索引
9. db.user_addr.dropIndex('Al.Em_1')

四. 查询
1. # 查找所有
2. db.foo.find()
3. # 查找一条记录
4. db.foo.findOne()
5. # 根据条件检索 10 条记录
6. db.foo.find({'msg':'Hello 1'}).limit(10)
7. #sort 排序
8. db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})
9. db.deliver_status.find().sort({'Ct':-1}).limit(1)
10. #count 操作
11. db.user_addr.count()
12. #distinct 操作 , 查询指定列,去重复
13. db.foo.distinct('msg')
14. #”>=” 操作
15. db.foo.find({"timestamp": {"$gte" : 2}})
16. # 子对象的查找
17. db.foo.find({'address.city':'beijing'})

五. 管理
1. # 查看 collection 数据的大小
2. db.deliver_status.dataSize()
3. # 查看 colleciont 状态
4. db.deliver_status.stats()
5. # 查询所有索引的大小
6. db.deliver_status.totalIndexSize()

六. 高级查询
条件操作符
$gt : >
$lt : <
$gte: >=
$lte: <=
$ne : != 、 <>
$in : in
$nin: not in
$all: all
$not: 反匹配 (1.3.3 及以上版本 )
查询 name <> "bruce" and age >= 18 的数据
db.users.find({name: {$ne: "bruce"}, age: {$gte: 18}});
查询 creation_date > '2010-01-01' and creation_date <= '2010-12-31' 的数据
db.users.find({creation_date:{$gt:new Date(2010,0,1), $lte:new Date(2010,11,31)});
查询 age in (20,22,24,26) 的数据
db.users.find({age: {$in: [20,22,24,26]}});
查询 age 取模 10 等于 0 的数据
db.users.find('this.age % 10 == 0');
或者
db.users.find({age : {$mod : [10, 0]}});
匹配所有
db.users.find({favorite_number : {$all : [6, 8]}});
可以查询出 {name: 'David', age: 26, favorite_number: [ 6, 8, 9 ] }
可以不查询出 {name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] }
查询不匹配 name=B* 带头的记录
db.users.find({name: {$not: /^B.*/}});
查询 age 取模 10 不等于 0 的数据
db.users.find({age : {$not: {$mod : [10, 0]}}});
# 返回部分字段
选择返回 age 和 _id 字段 (_id 字段总是会被返回 )
db.users.find({}, {age:1});
db.users.find({}, {age:3});
db.users.find({}, {age:true});
db.users.find({ name : "bruce" }, {age:1});
0 为 false, 非 0 为 true
选择返回 age 、 address 和 _id 字段
db.users.find({ name : "bruce" }, {age:1, address:1});
排除返回 age 、 address 和 _id 字段
db.users.find({}, {age:0, address:false});
db.users.find({ name : "bruce" }, {age:0, address:false});
数组元素个数判断
对于 {name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] } 记录
匹配 db.users.find({favorite_number: {$size: 3}});
不匹配 db.users.find({favorite_number: {$size: 2}});
$exists 判断字段是否存在
查询所有存在 name 字段的记录
db.users.find({name: {$exists: true}});
查询所有不存在 phone 字段的记录
db.users.find({phone: {$exists: false}});
$type 判断字段类型
查询所有 name 字段是字符类型的
db.users.find({name: {$type: 2}});
查询所有 age 字段是整型的
db.users.find({age: {$type: 16}});
对于字符字段,可以使用正则表达式
查询以字母 b 或者 B 带头的所有记录
db.users.find({name: /^b.*/i});
$elemMatch(1.3.1 及以上版本 )
为数组的字段中匹配其中某个元素
Javascript 查询和 $where 查询
查询 age > 18 的记录,以下查询都一样
db.users.find({age: {$gt: 18}});
db.users.find({$where: "this.age > 18"});
db.users.find("this.age > 18");
f = function() {return this.age > 18} db.users.find(f);
排序 sort()
以年龄升序 asc
db.users.find().sort({age: 1});
以年龄降序 desc
db.users.find().sort({age: -1});
限制返回记录数量 limit()
返回 5 条记录
db.users.find().limit(5);
返回 3 条记录并打印信息
db.users.find().limit(3).forEach(function(user) {print('my age is ' + user.age)});
结果
my age is 18
my age is 19
my age is 20
限制返回记录的开始点 skip()
从第 3 条记录开始,返回 5 条记录 (limit 3, 5)
db.users.find().skip(3).limit(5);
查询记录条数 count()
db.users.find().count();
db.users.find({age:18}).count();
以下返回的不是 5 ,而是 user 表中所有的记录数量
db.users.find().skip(10).limit(5).count();
如果要返回限制之后的记录数量,要使用 count(true) 或者 count( 非 0)
db.users.find().skip(10).limit(5).count(true);
分组 group()
假设 test 表只有以下一条数据
{ domain: "www.mongodb.org"
, invoked_at: {d:"2009-11-03", t:"17:14:05"}
, response_time: 0.05
, http_action: "GET /display/DOCS/Aggregation"
}
使用 group 统计 test 表 11 月份的数据 count:count(*) 、 total_time:sum(response_time) 、 avg_time:total_time/count;
db.test.group(
{ cond: {"invoked_at.d": {$gt: "2009-11", $lt: "2009-12"}}
, key: {http_action: true}
, initial: {count: 0, total_time:0}
, reduce: function(doc, out){ out.count++; out.total_time+=doc.response_time }
, finalize: function(out){ out.avg_time = out.total_time / out.count }
} );
[
{
"http_action" : "GET /display/DOCS/Aggregation",
"count" : 1,
"total_time" : 0.05,
"avg_time" : 0.05
}
]

七. 对数组根据条件查询
$ all、$size、$slice、$elemMatch
$ all查找数组中包含指定的值的文档
$ size 查找数组大小等于指定值的文档
$ slice可以查询数组中第几个到第几个
$ elemMatch 文档包含有一个元素是数组,那么$elemMatch可以匹配内数组内的元素并返回文档数据

  常见问题

  a.对于对象中含有数据结构只返回部分数组

  //按照开始位置、页长度,对mongo数据进行切片

  Query query = new Query(Criteria.where("_id").is(id));

      query.fields().slice(noteName, pageNo, pageSize);//java进行分片

原文地址:https://www.cnblogs.com/jiangds/p/6875717.html