一、查看数据库javascript
查看mongodb中的数据库(相似于mysql中的show databases);java
> show dbs local 0.000GB test 0.000GB
二、使用数据库mysql
若是使用的数据库不存在,mongodb会自动建立对应的数据库(而mysql须要create database <数据库名>)redis
> use testdb switched to db testdb
三、查看当前使用的数据库(相似于MySQL中的 select database();)
sql
> db testdb
四、删除数据库mongodb
> use testdb switched to db testdb > db.dropDatabase() { "dropped" : "testdb", "ok" : 1 }
mongodb中是分为集合和文档的。相似于 MySQL中的表和行的关系shell
一、插入文档(就是相似于插入MySQL表中的上数据,只是在mongodb中这里不须要直接建立对应的所谓的表)数据库
语法:db.<collection_name(集合名称,"所谓的表名")>.insert({"<键名>":"<键值>"......})函数
由于mongodb是数据文档型数据库,这种{}的结构基本上和javascript中的对象,Python中的字典,redis中的散列是相似的。说白了就是映射关系spa
> 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(值);
二、更新文档
语法: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=42 42 > db.collection2.save(x) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59, "num" : 42 } > x.num=43 43 > db.collection2.save(x) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59, "num" : 43 }
上面x不存在时候save就进行插入,x存在时候进行更新的操做
三、删除文档
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
四、查询操做
先说一下简单的一些条件格式吧,有点多,后面再继续写。。。。
等于 {<key>:<value>
}
小于 {<key>:{$lt:<value>}}
小于或等于 {<key>:{$lte:<value>}}
大于
{<key>:{$gt:<value>}}
大于或等于
{<key>:{$gte:<value>}}
不等于
{<key>:{$ne:<value>}}
下一篇开写吧