show dbs 查看所有数据库
use kgckb16 如果有库则被选中,如果没有也不会报错,延迟创建
db 查看当前正在使用的数据库
db.createCollection("kb16") 在当前库中创建表(集合)
show collections 查看当前库中的表
show tables
db.kb15.drop() 删除表(集合)
db.dropDatabase() 删除库
{ "dropped" : "kgckb16", "ok" : 1 }
db.kb16.insert({"id":1,"name":"cuicui"}) 隐式创建表
db.kb16.find() 查询全部
db.kb16.count() 查询表数据数量
#条件查询数量
db.comment.count({"name":"lisi"})
#多条件查询数量
db.comment.count({"_id":2,"title":"b2"})
//隐式批量插入数据
db.comment.insertMany(
... [{"title":"a1","content":"good","name":"zhangsan","nick":"xxs"},
... {"title":"b1","content":"hi","name":"lisi","nick":"xxs"},
... {"title":"c1","content":"ok","name":"wangwu","nick":"wxx"},
... {"title":"d1","content":"yes","name":"zhaoliu","nick":"znn"},
... {"title":"e1","content":"no","name":"zhangsan","nick":"xxs"},
... {"title":"f1","content":"maybe","name":"lisi","nick":"xxs"}]
... )
//查询 1:显示;0:不显示
db.comment.find({"title":"b1"})
#查询前三条数据
db.comment.find().limit(3)
#查询跨过前两条的三条数据
db.comment.find().limit(3).skip(2)
分页查询 pageNum(总页数) pageCount(每页显示的条数)
1页 limit(pageCount).skip(0)
2页 limit(pageCount).skip(pageCount)
3页 limit(pageCount).skip(2*pageCount)
n页 limit(pageCount).skip((n-1)*pageCount)
db.comment.find({"name":"zhangsan"})
//select _id,title from comment where name ="zhangsan"
db.comment.find({"name":"zhangsan"},{"title":1})
db.comment.find({"name":"zhangsan"},{"title":1,"name":1})
//select title from comment where name ="zhangsan"
db.comment.find({"name":"zhangsan"},{"title":1,"name":1,"_id":0})
#删除
db.comment.remove({"title":"a1"})
WriteResult({ "nRemoved" : 1 })
#条件删除
db.comment.remove({"name":"lisi"})
WriteResult({ "nRemoved" : 2 })
db.comment.remove({"_id": ObjectId("625f58294090e7c8473513f1")})
WriteResult({ "nRemoved" : 1 })
#删除所有数据
db.comment.remove({})
#删除表
db.comment.drop()
db.comment.insertMany(
[{ "_id" :1, "title" : "a1", "content" : "good", "name" : "zhangsan", "nick" : "xxs" },
{ "_id" : 2, "title" : "b1", "content" : "hi", "name" : "lisi", "nick" : "xxs" },
{ "_id" :3, "title" : "c1", "content" : "ok", "name" : "wangwu", "nick" : "wxx" },
{ "_id" : 4, "title" : "d1", "content" : "yes", "name" : "zhaoliu", "nick" : "znn" },
{ "_id" :5, "title" : "e1", "content" : "no", "name" : "zhangsan", "nick" : "xxs" },
{ "_id" :6, "title" : "f1", "content" : "maybe", "name" : "lisi", "nick" : "xxs" }]
)
#更新
#覆盖修改
db.comment.update({"_id":1},{"title":"a11"})
#局部修改
db.comment.update({"_id":2},{$set:{"content":"hi fine"}})
#默认修改匹配到的第一条数据
db.comment.update({"name":"lisi"},{$set:{"nick":"ls"}})
#更新所有匹配到的数据 {multi:true}
db.comment.update({"name":"lisi"},{$set:{"nick":"ls"}},{multi:true})
db.comment.insertMany(
[{ "_id" :1, "title" : "a1","count":11, "content" : "good", "name" : "zhangsan", "nick" : "xxs" },
{ "_id" : 2, "title" : "b1","count":12, "content" : "hi", "name" : "lisi", "nick" : "xxs" },
{ "_id" :3, "title" : "c1", "count":13,"content" : "ok", "name" : "wangwu", "nick" : "wxx" },
{ "_id" : 4, "title" : "d1","count":14, "content" : "yes", "name" : "zhaoliu", "nick" : "znn" },
{ "_id" :5, "title" : "e1","count":15, "content" : "no", "name" : "zhangsan", "nick" : "xxs" },
{ "_id" :6, "title" : "f1","count":16, "content" : "maybe", "name" : "lisi", "nick" : "xxs" }])
#自增一
db.comment.update({"_id":1},{$inc:{"count":NumberInt(1)}})
#排序
db.comment.find().sort({"_id":1}) //升序
db.comment.find().sort({"_id":-1}) //降序
db.comment.find({},{"count":1,"title":1}).sort({"count":1})
MongoDB $关键字关系比较符:
小于:$lt
小于或等于:$lte
大于:$gt
大于或等于:$gte
不等于:$ne
属于:$in
与:and
或:or
db.comment.find({"name":"zhangsan"})
db.comment.find({"name":"lisi"})
db.comment.find({"name":{$in:["zhangsan","lisi"]}}) in
db.comment.find({"count":{$lt:15}})
db.comment.find({"count":{$lte:15}})
db.comment.find({"count":{$gt:15}})
db.comment.find({"count":{$gte:15}})
db.comment.find({"count":{$ne:15}})
db.comment.find({$and:[{"count":{$gt:13}},{"count":{$lt:15}}]}) and
db.comment.find({"count":{$gt:13,$lt:15}}) equals and
db.comment.find({$and:[{"name":"zhangsan"},{"count":{$lt:15}}]}) count<15 and name=zhangsan
db.comment.find({$or:[{"name":"zhangsan"},{"count":{$lt:15}}]}) count<15 or name=zhangsan
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)