欢迎来到李先生的博客

深山的鹿,不知归处;万般皆苦,只可自渡。
扩大
缩小

MongoDB基础知识(二)

一、基本概念
1:文档(document)是MongoDB中数据的基本单元,非常类似于关系型数据库管理系统中的行
2:集合(collection)可以看做是一个拥有动态模式(dynamic schema)的表
3:MongoDB的一个实例可以拥有多个相互独立的数据库,每一个数据库拥有自己的集合
4:每一个文档都有一个特殊的键"_id",这个键在文档所属的集合中是唯一的。
5:MongoDB自带JavaScript shell,可用于管理MongoDB的实例或数据操作
 
可以看两张图,比较好理解,这是从 http://www.runoob.com/ 这里看到的
 
 

 

 
 
二、shell基本操作
四种基本操作:创建、读取、更新、删除,就是平常说的增删改查
1):创建    insert函数可将一个文档添加到集合中,如:
    > show dbs;
    admin 0.000GB
    local 0.000GB
    > use testdb;
    switched to db testdb
    > post={"title":"My Blog Post",
    ... "content":"HaHa",
    ... "date":new Date()}
    {
    "title" : "My Blog Post",
    "content" : "HaHa",
    "date" : ISODate("2017-07-07T03:20:44.698Z")
    }
    > db.blog.insert(post)
    WriteResult({ "nInserted" : 1 })
    > db.blog.find()
    { "_id" : ObjectId("595efe1af713ea7372854b9c"), "title" : "My Blog Post", "content" : "HaHa", "date" : ISODate("2017-07-07T03:20:44.698Z") }

 

 
2)读取: find和findOne方法用于查询集合里的文档,若只想看一个文档,可以用findOne,上面例子的查询
      find查询的时候最多显示20个匹配的文档
    > db.blog.findOne()
    {
    "_id" : ObjectId("595efe1af713ea7372854b9c"),
    "title" : "My Blog Post",
    "content" : "HaHa",
    "date" : ISODate("2017-07-07T03:20:44.698Z")
    } 

 

3)更新: 使用update修改,update接受至少两个参数,第一个是限定条件(用于匹配待更新的文档),第二个是新的文档。假设,在刚刚的基础上,添加评论功能,使用数组存储
    > post.comments=[]
    [ ]
    > db.blog.update({"_id":ObjectId("595efe1af713ea7372854b9c")},post)
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.blog.findOne()
    {
    "_id" : ObjectId("595efe1af713ea7372854b9c"),
    "title" : "My Blog Post",
    "content" : "HaHa",
    "date" : ISODate("2017-07-07T03:20:44.698Z"),
    "comments" : [ ]
    }
可以通过查询看到,已经添加了comments键
 
4)删除: 使用remove方法可将文档从数据库中永久的删除,如果没有使用参数匹配,那么则会删除集合内的所有文档。它可以接受一个作为限定条件的文档作为参数
    > db.blog.remove({"_id":ObjectId("595efe1af713ea7372854b9c")})
    WriteResult({ "nRemoved" : 1 })
    > db.blog.findOne()
    null
可以看到,集合内没有文档了
 
 
三:数据类型
null:用于表示空值或者不存在的字段   {"x":null}
布尔型:布尔型有两个值true和false      {"x":true}
数值:
         shell默认使用64位浮点型数值       {"x":3.14}   or   {"x":3}
         对于整型值,可使用NumberInt类(表示4字节带符号整数)      {"x":NumberInt("3")}
                  NumberLong类(表示8字节带符号整数)   {"x":NumberLong("3")}
 
字符串:UTF-8字符串都可以表示为字符串类型的数据           {"x":"foobar"}
日期:日期被存储为自新世纪元以来经过的毫秒数,不存储时区   {"x":new Date}
正则表达式:查询时,使用正则表达式作为限定条件              {"x":/foobar/i}
数组:数据列表或数据集可以表示为数组         {"x":["a","b","c","d"]}
内嵌文档:文档可嵌套其他文档           {"x":{"foo":"bar"}}
对象id:对象id是一个12字节的ID,是文档的唯一标识
 
MongoDB shell的技巧:
在连接进入mongo后,可以使用help来帮助查看
    > help
    db.help() help on db methods
    db.mycoll.help() help on collection methods
    sh.help() sharding helpers
    rs.help() replica set helpers
    help admin administrative help
    help connect connecting to a db help
    help keys key shortcuts
    help misc misc things to know
    help mr mapreduce
    show dbs show database names
    show collections show collections in current database
    show users show users in current database
    show profile show most recent system.profile entries with time >= 1ms
    show logs show the accessible logger names
    show log [name] prints out the last segment of log in memory, 'global' is default
    use <db_name> set current database
    db.foo.find() list objects in collection foo
    db.foo.find( { a : 1 } ) list objects in foo where a == 1
    it result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x set default number of items to display on shell
    exit quit the mongo shell
 
查看一个函数的源代码,可以直接使用,如:db.test.update 就会把update函数给显示出来
 

posted on 2017-07-07 16:54  Captain_Li  阅读(631)  评论(0编辑  收藏  举报

导航