数据库操作 1、查看数据库 查看mongodb中的数据库(类似于mysql中的show databases); >showdbslocal0.000GBtest0.000GB 2、使用数据库 如果使用
数据库操作
1、查看数据库
查看mongodb中的数据库(类似于mysql中的show databases);
> show dbslocal 0.000GBtest 0.000GB
2、使用数据库
如果使用的数据库不存在,mongodb会自动创建对应的数据库(而mysql需要create database <数据库名>)
> use testdbswitched to db testdb
3、查看当前使用的数据库(类似于MySQL中的 select database();)
> dbtestdb
4、删除数据库
> use testdbswitched to db testdb> db.dropDatabase(){ "dropped" : "testdb", "ok" : 1 }
文档操作
mongodb中是分为集合和文档的。类似于 MySQL中的表和行的关系
1、插入文档(就是类似于插入MySQL表中的上数据,只是在mongodb中这里不需要直接创建对应的所谓的表)
语法:db.<collection_name(集合名称,"所谓的表名")>.insert({"<键名>":"<键值>"......})
因为mongodb是数据文档型数据库,这种{}的结构基本上和javascript中的对象,Python中的字典,redis中的散列是类似的。说白了就是映射关系
> db.collection1.insert({"name":"xiaoming"})WriteResult({ "nInserted" : 1 })> db.collection1.insert({"name":"xiaoming2"})WriteResult({ "nInserted" : 1 })> db.collection1.insert({"name":"xiaoming3","tel":"10086"})WriteResult({ "nInserted" : 1 })> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaoming2" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086"
上面的是在集合collection1中插入的三个文档,就是类似的键值对应键名类似mysql中insert into tablename (列名)value(值);
2、更新文档
语法:db.<集合名>.update({"键值":"键名"},{$set:{"键值":"键名"}})前面是条件,后面是内容
类似于MySQL中 update collection1 set 列名=列值 where 列名=列值;
> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaoming2" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }> db.collection1.update({"name":"xiaoming2"},{$set:{"name":"xiaohong"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection1.update({"name":"xiaoming"},{$set:{"tel":"123456"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }
上面的例子可以看出改一个不存在的记录来对记录进行新增的操作。
对多行记录进行修改
> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong1" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong1" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong1" }> db.collection1.update({"name":"xiaohong1"},{$set:{"name":"xiaohong2"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong1" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong1" }> db.collection1.update({"name":"xiaohong1"},{$set:{"name":"xiaohong2"}},false,true)WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" }>
默认的update只对一行进行修改,需要在要修改的记录后面加入参数 false,true
其实具体的含义就是 False找不到就进行插入操作,true进行修改多行
进行递增和递减的操作 $inc
> db.collection2.insert({"grade":60})WriteResult({ "nInserted" : 1 })> db.collection2.find(){ "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 60 }> db.collection2.update({"grade":60},{$inc:{"grade":2}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection2.find(){ "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 62 }> db.collection2.update({"grade":62},{$inc:{"grade":-3}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection2.find(){ "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59 }
删除某一列使用参数 $unset
> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" }> db.collection1.update({"name":"xiaohong"},{$unset:{"name":"xiaohong2"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676") }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" }
save是一个shell函数,在文档中不存在时插入,存在时更新
语法:db.<集合名>.save()
> db.collection2.find(){ "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59 }> x.num=4242> db.collection2.save(x)WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection2.find(){ "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59, "num" : 42 }> x.num=4343> db.collection2.save(x)WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.collection2.find(){ "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59, "num" : 43 }
上面x不存在时候save就进行插入,x存在时候进行更新的操作
3、删除文档
Db.<集合名>.remove({"键值":"键名"})(删除的where语句。)
Db.<集合名>.drop()(删除所有的行)
> db.collection1.find(){ "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" }{ "_id" : ObjectId("56e42d25cd979329c402b676") }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" }> db.collection1.remove({"name":"xiaoming"})WriteResult({ "nRemoved" : 1 })> db.collection1.find(){ "_id" : ObjectId("56e42d25cd979329c402b676") }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }{ "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" }{ "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" }> db.collection1.remove({"name":"xiaohong2"})WriteResult({ "nRemoved" : 3 })> db.collection1.find(){ "_id" : ObjectId("56e42d25cd979329c402b676") }{ "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }> db.collection1.drop()true
4、查询操作
先说一下简单的一些条件格式吧,有点多,后面再继续写。。。。
等于 {<key>:<value>
}
小于 {<key>:{$lt:<value>}}
小于或等于 {<key>:{$lte:<value>}}
大于
{<key>:{$gt:<value>}}
大于或等于
{<key>:{$gte:<value>}}
不等于
{<key>:{$ne:<value>}}
下一篇开写吧