插入数据
db.comment.insert(
{"articleid":"100000","content":"今天天气真好,阳光明媚","userid":"1001",
"nickname":"Rose","age":"20","phone":"18807141995","createdatetime":new Date(),
"likenum":NumberInt(10),"state":null}
)
db.comment.insertMany([{"_id":"1","articleid":"100001","content":"清晨,我们不该把事件浪费在手机上,健康很重要,喝一杯温水,幸福你我他。","userid":"1002","nickname":"相忘于江湖","age":"25","phone":{"homePhone":"82174911","mobilePhone":"13065840128"},"createdatetime":new Date("2020-01-02 09:08:15"),"likenum":NumberInt(1000),"state":"1"},{"_id":"2","articleid":"100001","content":"我夏天空腹喝凉开水,冬天喝温开水","userid":"1003","nickname":"伊人憔悴","age":"22","phone":"13442031624","createdatetime":new Date("2020-01-02 10:20:40"),"likenum":NumberInt(888),"state":"1"},{"_id":"3","articleid":"100001","content":"夏天和冬天,我都喝凉开水","userid":"1004","nickname":"杰克船长","age":"28","phone":"1393716334","createdatetime":new Date("2020-01-02 19:56:09"),"likenum":NumberInt(666),"state":null},{"_id":"4","articleid":"100001","content":"专家说不能空腹喝冰水,影响健康","userid":"1005","nickname":"罗密欧","age":"18","phone":"15813134403","createdatetime":new Date("2020-01-03 11:26:29"),"likenum":NumberInt(2000),"state":"1"},{"_id":"5","articleid":"100001","content":"研究表明,刚烧开的水千万不要喝,因为烫嘴","userid":"1005","nickname":"罗密欧","age":"18","phone":"15813134403","createdatetime":new Date("2020-01-03 15:10:37"),"likenum":NumberInt(3000),"state":"1"},{"_id":"6","articleid":"100001","content":"喝水是生命体通过口腔摄入水分的方式,人体每天通过口腔摄入的液体大概有2升","userid":"1006","nickname":"爱德华","age":"30","phone":{"homePhone":"62771541","mobilePhone":"13262984142"},"createdatetime":new Date("2020-01-03 15:10:37"),"likenum":NumberInt(3000),"state":"1"}
])
更新数据
db.comment.update({"content":"喝水是生命体通过口腔摄入水分的方式,人体每天通过口腔摄入的液体大概有2升"},{$set:{"content":"喝水增加了尿量,能使有害物质及时排出体内"}})
文档查询
#按条件查询文档
db.comment.find({$and:[{"userid":"1005","nickname":"罗密欧"}]}).pretty()
db.comment.find({$or:[{"userid":"1002","userid":"1003"}]}).pretty()
db.comment.find({"userid":{$gt:"1005"}}).pretty()
db.comment.find({"userid":{$lt:"1004"}}).pretty()
db.comment.find({"userid":{$gte:"1005"}}).pretty()
db.comment.find({"userid":{$lte:"1003"}}).pretty()
db.comment.find({"userid":{$ne:"1005"}}).pretty()
db.comment.find({"_id":{$in:["1","3"]}}).pretty()
db.comment.find({"_id":{$nin:["1","3","5"]}}).pretty()
#按特定类型查询文档
db.comment.find({"state":null}).pretty()
db.comment.find({"content":/^专家/}).pretty()
db.comment.find({"phone":{"homePhone":"62771541","mobilePhone":"13262984142"}}).pretty()
db.comment.find({"phone.homePhone":"82174911"}).pretty()
聚合管道操作
db.comment.aggregate([{$group:{"_id":"$userid"}}]).pretty()
db.comment.aggregate({$limit:3}).pretty()
db.comment.aggregate([{$match:{"nickname":"罗密欧"}}]).pretty()
db.comment.aggregate([{$sort:{"age":-1}}]).pretty()
db.comment.aggregate([{$project:{"_id":0}}]).pretty()
db.comment.aggregate({$skip:4}).pretty()
#创建一个集合product
db.createCollection("product")
show collections
db.product.insertMany([
{"_id":"1","name":"iPhone8","price":3000,"type":"电子通信"},
{"_id":"2","name":"adidas neo","price":700,"type":"服装"},
{"_id":"3","name":"nike air max 90","price":760,"type":"服装"},
{"_id":"4","name":"HuaWei mate30","price":5000,"type":"电子通信"},
{"_id":"5","name":"vivo x27","price":2000,"type":"电子通信"},
])
db.product.aggregate([{$group:{"_id":"$type","price":{$sum:"$price"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$type","price":{$avg:"$price"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$type","price":{$min:"$price"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$type","price":{$max:"$price"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$type","tags":{$push:"$price"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$type","product":{$first:"$name"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$type","product":{$last:"$name"}}}]).pretty()
Map-Reduce操作
db.comment.mapReduce(
function(){emit(this.nickname,1);},
function(key,values){return Array.sum(values)},
{
query:{state:"1"},
out:"comment_total"
}
)
db.comment_total.find()
索引操作
#查看索引
db.comment.getIndexes()
#查看索引大小
db.comment.totalIndexSize()
#创建索引
db.comment.createIndex({userid:1})
db.comment.getIndexes()
db.comment.createIndex({userid:1,nickname:-1})
db.comment.getIndexes()
#删除索引
db.comment.dropIndex({userid:1})
db.comment.getIndexes()
db.comment.createIndex({userid:1})
db.comment.dropIndexes()
db.comment.getIndexes()
1.NOSQL数据库习题/CSDN@煮酒、
2.NOSQL——第三章/CSDN@煮酒、
3.【NoSQL数据库技术与应用】-- MongoDB数据库操作/CSDN@⚆Pearl