第二篇 MongoDb的使用(linux)
1. 创建或切换到数据库
- 子厚的操作均是在testDb中执行,除非只用user函数切换到其他db
user testdb
2. 查看可用的数据库和集合
show dbs
show collections
3. 在集合中插入数据
db.testdb.insertOne({json(Bson)数据})
4. 查询数据
db.testdb.find()
db.testdb.findOne()
db.testdb.find({"name" : "trunk liu"})
db.testdb.find({"name" : "trunk liu"}, {"age" : 1})
db.testdb.find("family.sisterName" : "AName")
db.testdb.find({"Authos" : "membrey,peter"})
db.testdb.find().count()
5. 使用sort、limit和skip
db.testdb.find({"可添加条件" : "条件的值"}).sort({"字段" : 1})
db.testdb.find().limit(10)
db.testdb.find().skip(20)
6.去重&分组 || distinct & group
db.testdb.distinct("字段")
7.更新update
db.testdb.updateOne({"条件字段" : "value"}, {"updateFile" : "value", "file2" : "value"},{upsert : true})
db.testdb.updateMany({"条件字段" : "value"}, {"updateFile" : "value", "file2" : "value"},{upsert : true})
·····还有很多方法,暂不列举
8.重命名集合 & 删除数据
db.testdb.renameCollection("newName")
db.testdb.deleteOne({"key" : "value"})
9.索引
db.testdb.CreateIndex({name : 1})