(1)查看当前的数据库html
show dbs
(2)切换数据库mongodb
use databaseName
(3)查看当前数据库下的collection数据库
show tables/collections
Mongodb的库是隐式建立,能够use一个不存在的库,而后在该库下建立collection,便可建立库
(1)建立collectionjson
db.createCollection('collectionName')
(2)collection容许隐式建立小程序
db.collectionName.insert(document)
(3)删除collection微信小程序
db.collectionName.drop()
(4)删除database安全
db.dropDatabase()
(1)增:insert
mongodb存储的是文档, 文档是json格式的对象微信
db.collectionName.insert(document)
1.增长单篇文档post
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'} ] )
(2)删:remove
db.collection.remove(查询表达式,{justOne:true/false})
justOne:是否只删一行,默认为false
注意:
查询表达式依然是个json对象
查询表达式匹配的行,将被删掉
若是不写查询表达式,collections中的全部文档将被删掉
1.删除stu表中 sn属性值为'001'的文档
db.stu.remove({sn:'001'})
2.删除stu表中gender属性为m的文档,只删除1行
db.stu.remove({gender:'m',true})
(3)改:update
db.collection.update(查询表达式,新值,选项);
1.把news表中name值为QQ的文档改成{name:'MSN'}
db.news.update({name:'QQ'},{name:'MSN'})
结果: 文档中的其余列也不见了,改后只有_id和name列了,即新文档直接替换了旧文档,而不是修改
2.若是是想修改文档的某列,能够用$set关键字
db.collectionName.update(query,{$set:{name:'QQ'}})
(a)修改时的赋值表达式:
1.$set:修改某列的值
2.$unset:删除某个列
3.$rename:重命名某个列
4.$inc:增加某个列
5.$setOnInsert:当upsert为true时,而且发生了insert操做时,能够补充的字段
(b)Option的做用:
{upsert:true/false,multi:true/false}
upsert是指没有匹配的行,则直接插入该行(和MySQL中的replace同样)
1.若是有name='wuyong'的文档,将被修改。若是没有,将添加此新文档
db.stu.update({name:'wuyong'},{$set:{name:'junshiwuyong'}},{upsert:true})
2.没有_id=99的文档被修改,所以直接插入该文档
db.news.update({_id:99},{x:123,y:234},{upsert:true})
multi: 是指修改多行(即便查询表达式命中多行,默认也只改1行,若是想改多行,能够用此选项)
1.把news中全部age=21的文档,都修改
db.news.update({age:21},{$set:{age:22}},{multi:true});
(4)查: find, findOne
db.collection.find(查询表达式,查询的列) db.collections.find(表达式,{列1:1,列2:1})
1.查询全部文档,全部内容
db.stu.find()
2.查询全部文档,的gender属性(_id属性默认老是查出来)
db.stu.find({},{gendre:1})
3.查询全部文档的gender属性,且不查询_id属性
db.stu.find({},{gender:1, _id:0})
4.查询全部gender属性值为male的文档中的name属性
db.stu.find({gender:'male'},{name:1,_id:0})
查看更多:
开发一个微信小程序实例教程