zoukankan      html  css  js  c++  java
  • MongoDB权威指南(1) Getting Started

    安装

      解压缩出来就OK了,mongoDB缺省使用磁盘根目录的data文件夹和data\db文件夹,

      这两个文件夹需要手工建立,如果想使用其他路径,那么启动mongoDB的时候需要指明路径

    运行

      运行CMD打开控制台窗口,导航至mongodb的bin目录,运行mongod.exe,服务器就启动起来了,按ctrl-c结束程序。

      另开一个控制台窗口,导航至mongodb的bin目录,运行mongo.exe来启动shell,就链接到服务器了,缺省连接到test数据库。

    -------------------------------------------------------------------------------------------------------------

    OK,第一章讲mongoDB如何如何牛b的我们就隔过去啦,这里直接是第二章。

    1.一些基本概念

      document :mongoDB里边数据的基本单位,相当于关系数据库里的行

      collection: 相当于关系数据库的表,不过是没有数据结构定义的

      每个mongoDB的实例可以运行多个database,每个database有自己的collection和权限控制

      mongoDB拥有一个强大的javascript shell,用于管理数据库和操作数据

      每个document都有一个特殊的key:"_id",这个值在collection内是唯一的

    document

    document是一组有序的key/value对,使用json风格的数据。

    {"foo" : 3, "greeting" "Hello, world!"}

    key是个UTF-8字符串,value 可以是很多类型,甚至是一个嵌入的document。

    collection

    collection是一组document,它是无结构定义的,所以你可以把任何document存入一个collection里。

    subcollection

    一个习惯性的组织collection的方式,使用.号分隔,像命名空间。例如,程序里使用了一个blog,它可能包含一个collection叫blog.post和另外一个collection叫blog.authors,这仅仅是出于组织内容的目的,它们俩看起来像是blog的子集,实际上他们没有任何关系,甚至blog也许就是不存在的。

    database

    一个mongoDB的实例可以运行多个database,database之间是完全独立的,每个database有自己的权限,每个database存储于磁盘的不同文件。

    2. mongoDB shell

    shell本身就是一个javascript解释器,让我们来干点啥来看看

    可以进行数学运算

    > x = 200
    200
    > x / 5;
    40

    可以使用标准的javascript库

    > Math.sin(Math.PI / 2);
    1
    > new Date("2010/1/1");
    "Fri Jan 01 2010 00:00:00 GMT-0500 (EST)"
    > "Hello, World!".replace("World""MongoDB");
    Hello, MongoDB
    !

    甚至可以定义javascript函数

    > function factorial (n) {
    ... 
    if (n <= 1return 1;
    ... 
    return n * factorial(n - 1);
    ... }
    > factorial(5);
    120

    能够执行javascript确实很cool,当然这不是shell的全部功能。

    • 使用use命名切换数据库
    > use foobar
    switched to db foobar

    然后就可以查看db变量来看看当前数据库是啥

    > db
    foobar
    • 使用insert函数想collection插入document

    先创建一个本地变量叫post

    > post = {"title" : "My Blog Post",
    ... 
    "content" : "Here's my blog post.",
    ... 
    "date" : new Date()}
    {
    "title" : "My Blog Post",
    "content" : "Here's my blog post.",
    "date" : "Sat Dec 12 2009 11:23:21 GMT-0500 (EST)"
    }

    然后把它插入到叫blog的collection里边去

    > db.blog.insert(post)

    然后我们可以用find函数看看blog里边的内容

    > db.blog.find()
    {
    "_id" : ObjectId("4b23c3ca7525f35f94b60a2d"),
    "title" : "My Blog Post",
    "content" : "Here's my blog post.",
    "date" : "Sat Dec 12 2009 11:23:21 GMT-0500 (EST)"
    }

    里边自动加了个叫"_id"的key。

    • find返回collection里的书有document,如果只想查看一个使用findone
      > db.blog.findOne()
      {
      "_id" : ObjectId("4b23c3ca7525f35f94b60a2d"),
      "title" : "My Blog Post",
      "content" : "Here's my blog post.",
      "date" : "Sat Dec 12 2009 11:23:21 GMT-0500 (EST)"
      }

    find和findone都可以有查询条件,第4章查询里边会讲。

    • 使用update来更新document

    update函数要有至少两个参数,第一个是条件,第二个是新的document

    先给post变量加一个叫comments的key,给它一个空数组做value。

    > post.comments = []
    [ ]

    执行更新,替换掉title是“My Blog Post”的那个document

    > db.blog.update({title : "My Blog Post"}, post)

    看看结果

    > db.blog.find()
    {
    "_id" : ObjectId("4b23c3ca7525f35f94b60a2d"),
    "title" : "My Blog Post",
    "content" : "Here's my blog post.",
    "date" : "Sat Dec 12 2009 11:23:21 GMT-0500 (EST)"
    "comments" : [ ]
    }
    • 使用remove删除document
      > db.blog.remove({title : "My Blog Post"})
      现在这个collection就又空了。

    3.基本数据类型

    • null
      表示一个空值或者不存在的字段
    • boolean
    • 32位整数
      shell中无法表示,javascript只支持64位浮点小数,所以会被转化为64位浮点小数。
    • 64位整数
      同上
    • 64位浮点小数
    • 字符串
    • symbol
      shell不支持此类型,来自数据库中的symbol类型数据会转化为字符串
    • object id
      0 1 2 3     |4 5 6     |7 8   |9 10 11
      Timestamp|Machine | PID  |Increment
    • date
    • 正则表达式
    • code
    • 二进制数据
    • maximum value
      bson有这样一个专门的类型来表示可能的最大值,shell不支持此类型。
    • minimum value
    • undefined
    • array
    • embeded document
  • 相关阅读:
    HDU 1358 Period (KMP)
    POJ 1042 Gone Fishing
    Csharp,Javascript 获取显示器的大小的几种方式
    css text 自动换行的实现方法 Internet Explorer,Firefox,Opera,Safar
    Dynamic Fonts动态设置字体大小存入Cookie
    CSS Image Rollovers翻转效果Image Sprites图片精灵
    CSS three column layout
    css 自定义字体 Internet Explorer,Firefox,Opera,Safari
    颜色选择器 Color Picker,Internet Explorer,Firefox,Opera,Safar
    CSS TextShadow in Safari, Opera, Firefox and more
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2458124.html
Copyright © 2011-2022 走看看