mongodb学习笔记

基本概念

  1. 文档
    1 . 文档中的键/值对是有序的。
    2 . 文档中的值不仅可以是在双引号里面的字符串,还可以是其他几种数据类型(甚至可以是整个嵌入的文档)。
    3 . MongoDB区分类型和大小写。
    4 . MongoDB的文档不能有重复的键。
    5 . 文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。
  2. 集合 - 集合就是 MongoDB 文档组,类似于 RDBMS中的表格
    1 . 集合名不能是空字符串”“。
    2 . 集合名不能含有\0字符(空字符),这个字符表示集合名的结尾。
    3 . 集合名不能以”system.”开头,这是为系统集合保留的前缀。
    4 . 用户创建的集合名字不能含有保留字符。有些驱动程序的确支持在集合名里面包含,这是因为某些系统生成的集合中包含该字符。除非你要访问这种系统创建的集合,否则千万不要在名字里出现$。 
  3. capped collections - 固定大小的collection,它有很高的性能以及队列过期的特性,高性能自动的维护对象的插入顺序,必须要显式的创建一个capped collection
    db.createCollection(“mycoll”, {capped:true, size:100000})
  4. 元数据 - 在MongoDB数据库中名字空间 .system.* 是包含多种系统信息的特殊集合(Collection) ?

基本命令

命令含义对应的SQL语句
show dbs显示所有数据的列表
show tables显示所有集合
db显示当前数据库对象或集合
use连接到一个指定的数据库,如数据库不存在,则创建
db.mytest.insert({x:100})在集合mytest中插入x值为100
db.mytest.find()查询集合mytest中的所有文档select * from mytest
db.mytest.findOne()查询集合mytest中的第一个文档select top 1* from mytest
db.mytest.find({“_id”:10001,”count”:1})select * from mytest where _id=10001 and count=1
db.mytest.find({“count”:{“$gt”:2,”$lte”:4}})select * from mytest where count>2 and count<=4
stime = new Date();db.news.find({“time”:{“$lt”:stime}})select * from news where time< ISODate(“2011-09-05T13:57:21.812Z”)
db.news.find({“count”:{“$gt”:2,”$lte”:4}},{“_id”:0,”time”:1,”news”:1})select time,news from news where count>2 and count<=4
db.getCollection(‘parambackup’).find({$or:[{backupid:1},{backupid:3}], “mac”:”000EA93D00CD”})select *from parambackup where mac=”000EA93D00CD” and (backupid=1 or backupid=3)
db.getCollection(‘devices’).find({“producttype”:/om/i})select *from devices where producttype like ‘%om%’ i表示不区分大小写
db.getCollection(‘devices’).find({“producttype”:/^om/i})select *from devices where producttype like ‘om%’
db.getCollection(‘devices’).find({‘alarmrule’:{$nin:[”,null]}})删除文档select * from devices where alarmrule != ” and alarmrule != null
db.dropDatabase()删除当前数据库
db.col.update({‘title’:’MongoDB教程’},{$set:{‘title’:’MongoDB’}},{multi:true})更新为MongoDB,注意:没有multi设置的话,只会更新第一条
db.dropDatabase()删除当前数据库
db.collection.drop()删除集合
db.collection.remove删除文档

搜索语句

关键字

“$lt”,”$lte”,”$gt”,”$gte”分别对应<,<=,>,>=
条件搜索:$in, $or, $nin
db.users.find({"$or":[ {"pageViews":{"$in":[10000,"refactor"]}}, {"url":"xxxx"}]})
注意: 并列的语句即是“与”条件,使用普通的and查询时,要尽量将最苛刻的条件放在前面
* “$not”可以用在任何条件之上
* null可以匹配自身,而且可以匹配”不存在的”

正则表达式(模糊查找)

db.users.find({"name":/^refact/i}) i-不区分大小写 ^-从开头匹配

聚合aggregate、管道

管道的概念类似linux中的管道,前者的输出为后者的输入
db.getCollection('metricHistory').aggregate([{$match:{$or:[{"metric_history_level":"serios"}]}},
{$group : {_id : "$metric_history_producer", last_time : {$last : "$metric_history_time"},last_level:{$last : "$metric_history_level"}}},
{$match:{_id:"000EA9451835"}}])

该语句先查找,再分组,再在分组中查找,其中$match的用法类似于find。
以下是在聚合框架可能的阶段 -
- $project - 用于从集合中选择一些特定字段。
- $match - 这是一个过滤操作,因此可以减少作为下一阶段输入的文档数量。
- $group - 这是上面讨论的实际聚合。
- $sort - 排序文档。
- $skip - 通过这种方式,可以在给定数量的文档的文档列表中向前跳过。
- $limit - 限制从当前位置开始的给定数量的文档数量。
- $unwind - 用于展开正在使用数组的文档。使用数组时,数据是预先加入的,此操作将被撤销,以便再次单独使用文档。 因此,在这个阶段,将增加下一阶段的文件数量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值