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})
{ "_id" : "4", "title" : "dd" }
> db.students.find({},{title:1,conent:1})
{ "_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)
{ "_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)
{ "_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})
{ "_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/})
{ "_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/})
{ "_id" : "3", "title" : "cc", "conent" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
> db.students.find({readNum:{$gt:25}})
{ "_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"]}})
{ "_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}}]})
{ "_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"})
> db.students.update({title:"aa"},{$set:{conent:"hehe"}})
> db.students.update({title:"aa"},{$set:{conent:"hehe"}},{multi:true})
>db.students.getIndexes()
>db.students.createIndex({userid:1})
>db.students.dropIndex("userid_1")
>db.students.dropIndexes()