一、数据语句
1.1、增删改查
1.1.1、插入
插入一条
db.comment.insert({"articleid":"100000","content":"今天天气真好,阳光明 媚","userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null})
插入多条
db.comment.insertMany([ {"_id":"1","articleid":"100001","content":"我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我
他。","userid":"1002","nickname":"相忘于江湖","createdatetime":new Date("2019-08- 05T22:08:15.522Z"),"likenum":NumberInt(1000),"state":"1"},
{"_id":"2","articleid":"100001","content":"我夏天空腹喝凉开水,冬天喝温开水","userid":"1005","nickname":"伊人憔 悴","createdatetime":new Date("2019-08-05T23:58:51.485Z"),"likenum":NumberInt(888),"state":"1"},
{"_id":"3","articleid":"100001","content":"我一直喝凉开水,冬天夏天都喝。","userid":"1004","nickname":"杰克船 长","createdatetime":new Date("2019-08-06T01:05:06.321Z"),"likenum":NumberInt(666),"state":"1"},
{"_id":"4","articleid":"100001","content":"专家说不能空腹吃饭,影响健康。","userid":"1003","nickname":"凯 撒","createdatetime":new Date("2019-08-06T08:18:35.288Z"),"likenum":NumberInt(2000),"state":"1"},
{"_id":"5","articleid":"100001","content":"研究表明,刚烧开的水千万不能喝,因为烫 嘴。","userid":"1003","nickname":"凯撒","createdatetime":new Date("2019-08- 06T11:01:02.521Z"),"likenum":NumberInt(3000),"state":"1"}
]);
错误处理
插入时指定了 _id ,则主键就是该值。
如果某条数据插入失败,将会终止插入,但已经插入成功的数据不会回滚掉。
try { db.comment.insertMany([
{"_id":"1","articleid":"100001","content":"我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我 他。","userid":"1002","nickname":"相忘于江湖","createdatetime":new Date("2019-08- 05T22:08:15.522Z"),"likenum":NumberInt(1000),"state":"1"},
{"_id":"2","articleid":"100001","content":"我夏天空腹喝凉开水,冬天喝温开水","userid":"1005","nickname":"伊人憔 悴","createdatetime":new Date("2019-08-05T23:58:51.485Z"),"likenum":NumberInt(888),"state":"1"},
{"_id":"3","articleid":"100001","content":"我一直喝凉开水,冬天夏天都喝。","userid":"1004","nickname":"杰克船 长","createdatetime":new Date("2019-08-06T01:05:06.321Z"),"likenum":NumberInt(666),"state":"1"},
{"_id":"4","articleid":"100001","content":"专家说不能空腹吃饭,影响健康。","userid":"1003","nickname":"凯 撒","createdatetime":new Date("2019-08-06T08:18:35.288Z"),"likenum":NumberInt(2000),"state":"1"},
{"_id":"5","articleid":"100001","content":"研究表明,刚烧开的水千万不能喝,因为烫 嘴。","userid":"1003","nickname":"凯撒","createdatetime":new Date("2019-08- 06T11:01:02.521Z"),"likenum":NumberInt(3000),"state":"1"}
]);
} catch (e) { print (e);
}
1.1.2、更新
覆盖更新,不管原纪录是啥直接用“{likenum:NumberInt(1001)}”覆盖
db.comment.update({_id:"1"},{likenum:NumberInt(1001)})
修改数据中的某一列数据
db.comment.update({_id:"2"},{$set:{likenum:NumberInt(889)}})
批量修改,更新userid=1003的所有数据
//修改所有符合条件的数据
db.comment.update({userid:"1003"},{$set:{nickname:"凯撒大帝"}},{multi:true})
1.1.3、列值修改
增加或减少某一列数值型数据,id为3的第一条数据likenum数据列增加1
db.comment.update({_id:"3"},{$inc:{likenum:NumberInt(1)}})
1.1.4、删除
删除_id=1的记录,如果{}内为空则删除所有数据
db.comment.remove({_id:"1"})
1.1.5、查询
查询所有
db.collection.find()
db.collection.find({})
按条件查询所有
db.collection.find({userid:'1003'})
按条件查询一条或多条
db.collection.findOne({userid:'1003'})
db.collection.find({userid:'1003'}).limit(NUMBER)
1.2、高级查询
1.2.1 数据统计
统计userid=1003的数据量
db.collection.count({userid:"1003"})
1.2.2 top查询结合跳过
模拟分页,6条数据分3页每页2行数据
db.collection.find().skip(0).limit(2)
db.collection.find().skip(2).limit(2)
db.collection.find().skip(4).limit(2)
1.2.3 排序(desc acs)
1为升序,-1为降序。注意查询方法优先级sort()> skip()>limit()
db.collection.find().sort({userid:-1,likenum:1})
1.2.4 正则查询
db.comment.find({content:/^专家/})
1.2.5 比较查询(<,>,>=,<=,!=)
db.集合名称.find({ "field" : { $gt: value }}) // 大于: field > value
db.集合名称.find({ "field" : { $lt: value }}) // 小于: field < value
db.集合名称.find({ "field" : { $gte: value }}) // 大于等于: field >= value
db.集合名称.find({ "field" : { $lte: value }}) // 小于等于: field <= value
db.集合名称.find({ "field" : { $ne: value }}) // 不等于: field != value
查询likenum列大于700的记录
db.collection.find({likenum:{$gt:NumberInt(700)}})
1.2.6 包含查询(in,not in)
集合中userid字段包含1003或1004的文档
db.collection.find({userid:{$in:["1003","1004"]}})
集合中userid字段不包含1003和1004的文档
db.collection.find({userid:{$nin:["1003","1004"]}})
1.2.7 组合查询(and,or)
集合中likenum大于等于700 且小于2000的文档
db.comment.find({$and:[{likenum:{$gte:NumberInt(700)}},{likenum:{$lt:NumberInt(2000)}}]})
集合中userid为1003,或likenum小于1000的文档记录
db.comment.find({$or:[ {userid:"1003"} ,{likenum:{$lt:1000} }]})