#1、创建集合Collection db.createCollection("article") db.createCollection("mx_article") #查看所有表 show collections #2、删除集合collection db.article.drop() #3、插入文档Document db.mx_article.insert( { "cid":1, "title":"标题001", "remark":"备注001", "content":"文章内容0001" } ) #3.2批量插入数据 document=( [{ "cid":10, "title":"标题010", "remark":"备注010", "content":"文章内容0010" }, { "cid":11, "title":"标题011", "remark":"备注011", "content":"文章内容0011" }, { "cid":12, "title":"标题012", "remark":"备注012", "content":"文章内容0012" } ] ) db.mx_article.insert(document) #4、查看集合里的文档内容--所有内容 db.mx_article.find({}) #5、修改文档内容 db.mx_article.update( {"cid":3}, {$set:{ "title":"标题003修改222" }}, {multi:true} ) #6、删除文档 db.mx_article.deleteOne({ "cid":101 }) #7、复杂的查询 #7.1 等于的判断 db.mx_article.find({ "cid":4,"title":"标题004" }) #7.2 大于和小于 db.mx_article.find( { "cid":{$gt:4,$lt:10} },{ "cid":1 }).explain() #7.3 and查询 db.mx_article.find({ "cid":{$gt:3}, "title":"标题008" }) db.mx_article.find({ "cid":3,"title":"标题008" }) #7、4 or 查询 db.mx_article.find({ $or:[{"cid":4},{"cid":"3"}] }) #7、5 limit 和skip db.mx_article.find({}).skip(3).limit(2).sort({"cid":-1}).explain() #7、6 group 和 sum #8 创建所有 db.mx_article.ensureIndex({"cid":1}) #show collections #查看mongodb版本号 db.version()