官方文档

1. 查询 - find 操作

1.1. find

db.getCollection("coll_name").find({'field_name':'value'},{'field_name':1,'field_name':1}) ;

第一个{} 放where条件 第二个{} 指定那些列显示和不显示 (0表示不显示 1表示显示)

where条件(第一个大括号中)

  • 等于:{‘name’ : ‘hurry’},key-value形式
  • and:{‘name’ : ‘hurry’, ‘age’ : 18},逗号分隔多个key-value
  • or:{ ‘$or’ : [{‘name’ : ‘hurry’}, {‘age’ : 18}] },关键字or,后面的列表里可以有多个条件
  • 比较符<, <=, >, >= ($lt, $lte, $gt, $ gte ) :{‘age’ : {‘mongoTemplate 查询不包含字符串 mongodb查询不包含某个字符_数组lte’ : 30}}
  • in和not in($in, $ nin) :db.getCollection(“core-user”).find({‘account’:{’$in’:[‘admin’,‘test’]}});
  • like:db.getCollection(“core-user”).find({‘account’:/ad/});,/ad/=like %ad%,/^ad/=like ad%
  • distinct去重:db.getCollection(“core-user”).distinct(‘account’)
  • count查询:db.getCollection(“core-user”).count() ;

1.2. 强大的$where查询

1.3. skip和limit

find().limit(NUMBER).skip(NUMBER)

  • limit:跳过的记录条数
  • skip:读取的记录条数

2. 更新 - update操作

2.1. update

update()命令: db.collection.update( criteria, objNew, upsert, multi )

  • criteria : update的查询条件,类似sql update查询内where后面的
  • objNew : update的对象和一些更新的操作符(如mongoTemplate 查询不包含字符串 mongodb查询不包含某个字符_mongodb_02inc…)等,也可以理解为sql update查询内set后面的
  • upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
  • multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

2.2. save

save()命令 :db.collection.save( x )

x就是要更新的对象,只能是单条记录。

如果在collection内已经存在一个和x对象相同的"_id"的记录。mongodb就会把x对象替换collection内已经存在的记录,否则将会插入x对象,如果x内没有_id,系统会自动生成一个再插入。相当于上面update语句的upsert=true,multi=false的情况。

2.2.1. mongodb的更新操作符(用在objNew位置)

  • $inc:{ $inc : { field : value } },对一个数字字段field增加value
  • $set:用法:{ $ set : { field : value } },就是相当于sql的set field = value,全部数据类型都支持$set
  • $unset:用法:{ $unset : { field : 1} },删除字段
  • $push:用法:{ $push : { field : value } },把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去
  • $pushAll:用法:{ $ pushAll : { field : value_array } },同$push,只是一次可以追加多个值到一个数组字段内
  • $addToSet:用法:{ $addToSet : { field : value } },增加一个值到数组内,而且只有当这个值不在数组内才增加。
  • $pop:删除数组内的一个值,
    用法:删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } },注意,只能删除一个值,也就是说只能用1或-1
  • mongoTemplate 查询不包含字符串 mongodb查询不包含某个字符_数组_03pull : { field : value } },从数组field内删除一个等于value值。
  • $pullAll:用法:{ $ pullAll : { field : value_array } },同$pull,可以一次删除数组内的多个值。

3. 增加 - insert 操作

3.1. insert

insert()

4. 删除 - remove操作

4.1. remove

remove(where),如果不带参数将删除所有数据!

5. 高级操作

5.1. 聚合

https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/

5.1.1. count(获取数量):用在查询语句的最后即可

5.1.2. distinct(去重):db.collection_name.distinct(field,query,options)

• field -----指定要返回的字段(string)
• query-----条件查询(document)
• options-----其他的选项(document)

5.1.3. group:

db.getCollection("aaa-my-first").aggregate(
	    [
	        {
	            $group: {
	                _id: { by: "$by"},// descriptions: {$push: "$description" } 不识别push为什么?
	                totalLike: { $sum: "$likes" },
	                averageLike: { $avg: "$likes" }
	            }
	        }
	    ]
	)

5.1.4. mapReduce:

5.2. 游标

5.3. Aggregate

pipeline分析框架

  • $lookup表关联:
    db.getCollection(“aaa-my-second”).aggregate([
    {
    $lookup:
    {
    from: “aaa-my-first”, //collection to join
    localField: “byF”,
    foreignField: “by”,
    as: “byDemo”
    }
    }
    ])
    "byDemo"作为一个字段出现。
  • $match筛选
  • $project 挑选字段
  • $unwind 拆分,{ $unwind: < field path> }
    db.getCollection(“aaa-my-second”).aggregate([{ mongoTemplate 查询不包含字符串 mongodb查询不包含某个字符_MongoDB_04tags" }])

5.4. 索引

  • 创建索引:唯一索引、组合索引
    db.inventory.createIndex( { type: 1 } )
  • 删除索引
  • 性能分析函数(explain)
    .explain( “executionStats” )

6. 其他概念

6.1. MongoDB 原子操作

mongodb不支持事务,所以,要注意这点。无论什么设计,都不要要求mongodb保证数据的完整性。

但是mongodb提供了许多原子操作,比如文档的保存,修改,删除等,都是原子操作。