MongoDB日常命令总结

本文档详细总结了MongoDB的常用操作,包括查看数据库、切换与创建数据库、操作集合(增删查改)、索引管理和数据过滤等。通过实例展示了如何插入、查询、更新和删除文档,以及如何进行数据排序、分页和条件查询。此外,还涉及到了数据过滤、正则表达式和条件运算符的使用,是MongoDB初学者和开发者的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MongoDB日常命令总结

> show databases    查看数据库
admin   0.000GB
config  0.000GB
kb11    0.000GB
local   0.000GB

> use kb11           //选择数据库
switched to db kb11

> show tables         //查看数据库中的表
students

> db.createCollection("aabb");  //创建表
{ "ok" : 1 }

> db.aabb.insert({"title":"aa","content":"bb","name":"cc","userid":"0001","nick":"kk"})
WriteResult({ "nInserted" : 1 })      // 插入数据

> db.aabb.find()
{ "_id" : ObjectId("60b97735fde1ba07ed72b762"), "title" : "aa", "content" : "bb", "name" : "cc", "userid" : "0001", "nick" : "kk" }       //查询表格

> db.aabb.count()           //查询表内数据数量
1

> db.aabb.drop()             //删除表格
true

> show tables                
students

> db.dropDatabase()              //删除库
{ "dropped" : "kb11", "ok" : 1 }

> use kgcdsj                     //创建库    如果库名不存在则直接创建新库
switched to db kgcdsj


> db.students.insertMany(                      //插入数据的同时创建表
... [
... {"_id":"1","title":"aa","conent":"good","readNum":21,"name":"a1","userid":"0001","nick":"gree"},
... {"_id":"2","title":"bb","conent":"hi","readNum":28,"name":"b1","userid":"0002","nick":"ant"},
... {"_id":"3","title":"cc","conent":"ok","readNum":27,"name":"c1","userid":"0003","nick":"plan"},
... {"_id":"4","title":"dd","conent":"no","readNum":29,"name":"a1","userid":"0001","nick":"gree"},
... {"_id":"5","title":"ee","conent":"yes","readNum":22,"name":"e1","userid":"0005","nick":"dog"}
... ]
... )
{
        "acknowledged" : true,
        "insertedIds" : [
                "1",
                "2",
                "3",
                "4",
                "5"
        ]
}


> db.students.findOne({name:"a1"})       //查询指定字段的数据,只显示一条
{
        "_id" : "1",
        "title" : "aa",
        "conent" : "good",
        "readNum" : 21,
        "name" : "a1",
        "userid" : "0001",
        "nick" : "gree"
}

> db.students.find({name:"a1"})               //查询符合条件的所有数据
{ "_id" : "1", "title" : "aa", "conent" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }

> db.students.find({name:"a1",conent:"no"})    //多个条件查询数据
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }

> db.students.find({name:"a1",conent:"no"},{title:1})   //多个条件查询数据显示指定内容,1为显示,0为不显示
{ "_id" : "4", "title" : "dd" }


> db.students.find({},{title:1,conent:1})          //只显示指定内容,类似于 select title,conent from students
{ "_id" : "1", "title" : "aa", "conent" : "good" }
{ "_id" : "2", "title" : "bb", "conent" : "hi" }
{ "_id" : "3", "title" : "cc", "conent" : "ok" }
{ "_id" : "4", "title" : "dd", "conent" : "no" }
{ "_id" : "5", "title" : "ee", "conent" : "yes" }


> db.students.remove({_id:"5"})             //删除表内数据
WriteResult({ "nRemoved" : 1 })

> db.students.find().limit(2)            //查询前两条记录,类似分页,每页显示2条
{ "_id" : "1", "title" : "aa", "conent" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "title" : "bb", "conent" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }


> db.students.find().limit(2).skip(2)    //查询跳过2条之后的 2条记录
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


> db.students.find().sort({readNum:1})                //指定字段 升序排列   1是升序,-1 降序
{ "_id" : "1", "title" : "aa", "conent" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "2", "title" : "bb", "conent" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


> db.students.find({},{name:1}).sort({readNum:-1}).limit(1)     //查询阅读量最多的作者姓名
{ "_id" : "4", "name" : "a1" }

> db.students.find({conent:/o/})             // conent  中包含“o”的数据
{ "_id" : "1", "title" : "aa", "conent" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


> db.students.find({conent:/^o/})             // conent  中以“o”开头的数据
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }

> db.students.find({readNum:{$gt:25}})         //阅读量大于25的数据,  $gt 大于   $lt 小于  $gte 大于等于    $lte  小于等于    $eq  等于    $ne  不等于
{ "_id" : "2", "title" : "bb", "conent" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


> db.students.find({userid:{$in:["0001","0002"]}})     //查询符合多个值条件的数据  $in $or $and
{ "_id" : "1", "title" : "aa", "conent" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "title" : "bb", "conent" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }



> db.students.find({$and:[{readNum:{$gt:21}},{readNum:{$lt:30}}]})    //阅读量大于21小于30的数据   和 $or 的用法格式一样
{ "_id" : "2", "title" : "bb", "conent" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "conent" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


>db.students.update({title:"bb"},{conent:"heihei"})  //替换,将原内容替换,原本一行的数据只有conent一个字段

> db.students.update({title:"aa"},{$set:{conent:"hehe"}})  //修改,只修改一行内容中的conent字段,其他字段保留  如果有多个符合条件的字段,可以用updateMany进行修改

> db.students.update({title:"aa"},{$set:{conent:"hehe"}},{multi:true})  //最后面加加入  {multi:true}  //也可以修改多条数据

>db.students.getIndexes()   //查询索引

>db.students.createIndex({userid:1})  //生成索引

>db.students.dropIndex("userid_1")     //删除索引

>db.students.dropIndexes()     //删除全部索引


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值