特别提醒:大小写敏感
show方法
showdbs 查看当前的数据库
show tables/collections 查看当前库下的collection
新建库
Mongodb的库是隐式创建,你可以use 一个不存在的库,然后在该库下创建collection,即可创建库
> use test
switched to db test
> db.createCollection('class')
{ "ok" : 1 }
local 0.000GB
test 0.000GB
删除库
db.dropDatabase();
新建集合
collection允许隐式创建,可以在use一个库之后,然后向一个集合插入一条数据,即可创建一个集合
> show collections
class
> db.news.insert({'name':'li','age':18})
WriteResult({ "nInserted" : 1 })
> show collections
class
news
删除集合
db.collectionName.drop()
数据插入
db.collectionName.isnert(document)
document:为json对象
> db.news.insert({'name':'lei','age':20})
WriteResult({ "nInserted" : 1 })
>
@1: 增加单篇文档
Db.collectionName.insert({title:’nice day’})
@2: 增加单个文档,并指定_id
Db.collectionName.insert({_id:8,age:78,name:’lisi’})
@3. 增加多个文档
db.collectionName.insert(
[
{time:'friday',study:'mongodb'},
{_id:9,gender:'male',name:'QQ'}
]
)
数据查询
db.collectionName.find(查询表达式,查询的列)
db.collections.find(表达式,{列1:1,列2:1});
1:查询所有文档 所有内容
>db.news.find()
{ "_id" : ObjectId("58b5079dfb99a979bf1999bf"), "name" : "li", "age" : 18 }
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lei", "age" : 20 }
2:查询指定的列,0:表示此列不查询,1:表示查询此列
> db.news.find({},{'_id':0})
{ "name" : "li", "age" : 18 }
{ "name" : "lei", "age" : 20 }
数据删除
db.collectionName.remove(查询表达式, 选项)
选项:是指 {justOne:true/false},是否只删一行, 默认为false
查询表达式:
1: 查询表达式依然是个json对象
2: 查询表达式匹配的行,将被删掉.
3: 如果不写查询表达式,collections中的所有文档将被删掉.
> db.news.remove({'name':'li'})
WriteResult({ "nRemoved" : 1 })
数据更新
db.collection.update(查询表达式,新值,选项);
特别注意:
新文档直接替换了旧文档,而不是修改,原有其它列将不存在
选项的作用:
{upsert:true/false,multi:true/false}
Upsert---是指没有匹配的行,则直接插入该行.(和mysql中的replace一样)
multi: 是指修改多行(即使查询表达式命中多行,默认也只改1行,如果想改多行,可以用此选项)
@1.替换目标文档
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lei", "age" : 20 }
> db.news.update({name:'lei'},{'name':'luse'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "luse" }
@2.$set 修改某列的值
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "age" : 25 }
> db.news.update({name:'lie'},{$set:{'age':18}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "age" : 18 }
@3.$rename 重命名某个列
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "age" : 18 }
> db.news.update({name:'lie'},{$rename:{'age':'top'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "top" : 18 }
@4.$inc 增加某个列的值
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "top" : 18 }
> db.news.update({name:'lie'},{$inc:{"top":2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "top" : 20 }
@5.$unset 删除某个列
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie", "top" : 20 }
> db.news.update({name:'lie'},{$unset:{'top':' '}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.news.find()
{ "_id" : ObjectId("58b5086cfb99a979bf1999c0"), "name" : "lie" }