Database
  • Introduction
  • Chapter1: MySQL
    • 1.1. MySQL在Max OSX上的筆記
      • 1.1.1.安裝My SQL (Mac OS X)
      • 1.1.2.My SQL在Mac上的一些warning message
        • 1.1.2.1.更改密碼
    • 1.2.MySQL for Beginners
      • 1.2.1.Getting the Development Environment Ready
        • 1.2.1.1.Windows console
      • 1.2.2.Beginning with MySQL Basics
        • 1.2.2.1.Creating a Database - CREATE DATABASE
        • 1.2.2.2.Creating a Table - CREATE TABLE
        • 1.2.2.3.Inserting Values into Table - INSERT INTO
        • 1.2.2.4.Retrieving Values from Table - SELECT
        • 1.2.2.5.Selecting Distinct Values from Column - DISTINCT
        • 1.2.2.6.Selecting a Specific Data using WHERE Clause
        • 1.2.2.7.Selecting with Multiple Identifiers Using AND
        • 1.2.2.8.Selecting with One of Many Identifiers Using OR
        • 1.2.2.9.Sorting the Data Using ORDER BY
        • 1.2.2.10.Limiting the Data Retrieved Using LIMIT
        • 1.2.2.11.Updating or Modifying Data in a Table Using UPDATE
        • 1.2.2.12.Deleting a Data from a Table Using DELETE
        • 1.2.2.13.Copy table
        • 1.2.2.14.Deleting a Table - DROP TABLE
        • 1.2.2.15.Deleting all values in Table - TRUNCATE TABLE
        • 1.2.2.16.Altering Table Structure - ALTER TABLE
      • 1.2.3.Time to Jump in for More
        • 1.2.3.1.Selecting Data with a Range - BETWEEN
        • 1.2.3.2.Creating Alias Name for a Column - AS
        • 1.2.3.3.Selecting Data which has one of Multitple Values - IN
        • 1.2.3.4.Using LIKE and WILDCARDS
        • 1.2.3.5.Copy Table with INSERT INTO SELECT
        • 1.2.3.6.Combine the Tables - UNION
      • 1.2.4.Few things that Keeps the Database in Shape
        • 1.2.4.1.Primary key
        • 1.2.4.2.Unique key
        • 1.2.4.3.Foreign key
        • 1.2.4.4.Default value
      • 1.2.5.Using Joins to Combine Tables
        • 1.2.5.1.Inner join
        • 1.2.5.2.Left join
        • 1.2.5.3.Right join
        • 1.2.5.4.Full join
  • Chapter2: MongoDB
  • Chapter3: SQL Server
    • 3.1.Obtain Query Execution Plan
    • 3.2.BCP
    • 3.3.SQL操作
      • 3.3.1.取代欄位, 更改欄位中的值
    • 3.4.產生create或drop database/table的script
Powered by GitBook
On this page
  • This section contain the following items:
  • 1.What is MongoDB?
  • 2.build MongoDB environment on Ubuntu (14.04)
  • 3.MongoDB debug message
  • 4.Backup and Restore
  • 5.Use mongoDb by using python
  • 6.MongoDB的基本操作

Was this helpful?

Chapter2: MongoDB

This section contain the following items:

  • 1.What is MongoDB?

  • 2.build MongoDB environment on Ubuntu (14.04)

  • 3.MongoDB debug message

  • 4.Backup and Restore

  • 5.Use mongoDb by using python

  • 6.MongoDB的基本操作

1.What is MongoDB?

2.build MongoDB environment on Ubuntu (14.04)

  1)echo “deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee /etc/apt/sources.list.d/mongo.list:

  建立/etc/apt/sources.list.d/mongo.list,並寫入deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen(作為操作的一部分,APT使用一個檔案列出可獲得套裝軟體的鏡像站台位址件- /etc/apt/sources.list.d

檔案中的各項資訊通常按如下格式列出:deb http://host/debian distribution section1 section2 section3)

  2)sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10:

    如果公鑰已經上傳到Key Server,可以使用下列指令下載公鑰:
      gpg --keyserver hkp://wwwkeys.us.pgp.net --recv-keys <金鑰指紋>

  3)sudo apt-get update:

    進行伺服器與用戶端的套件表頭清單更新, 在 apt-get update 後,再使用 apt-get dist-upgrade 這樣就能夠將整個系統升級

  4)sudo apt-get install mongodb-10gen

  5)ps -ef | grep mongo:

    確定 mongodb已在執行

  6)ls -ls /usr/bin | grep mongo

  7)ls -ls /etc/init.d | grep mongo

  8)sudo service mogodb start, sudo service mogodb stop, sudo service mogodb restart

3.MongoDB debug message

  1)ERROR: dbpath(data/db) does not exist

    sudo mkdir -p /data/db
    sudo useradd mongo
    sudo passwd mongo
    sudo chown -R mongo:mongo /data/db
    sudo chmod 0755 /data/db
    sudo chown -R $USER /data/db

    可以下ls -ld /data/db/
    ->理論上應該要是: drwxr-xr-x  7 gibber  wheel  238 Aug  5 11:07 /data/db/ (gibber是資料夾owner, wheel是gibber所屬的group)

   2)10309 Unable to create/open lock file: /data/db/mongod.lock errno:

    2014-10-06T12:59:35.802+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

    2014-10-06T12:59:35.802+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

    rm /var/lib/mongodb/mongod.lock
    sudo service mongod restart

    參考資料:
     1.http://stackoverflow.com/questions/26211671/failed-to-connect-to-127-0-0-127017-reason-errno111-connection-refused
    2.https://gist.github.com/adamgibbons/cc7b263ab3d52924d83b
    3.http://www.marcusoft.net/2014/05/mongodb-and-10309-unable-to-createopen.html
    4.http://stackoverflow.com/questions/24599119/mongodb-not-working-error-dbpath-data-db-does-not-exist

4.Backup and Restore

  Backup:
  1) Enter mongodb command mode by type "mongo" on terminal
  2) use admin
  3) db.runCommand({fsync:1,lock:1})
  4) db.currentOp()
  5) Ctrl + C to back to normal terminal mode 
  6) sudo mongodump -d vote -o ../data/backup
  7) Enter mongodb command mode by type "mongo" on terminal
  8) db.fsyncUnlock()

  Restore:
  1)sudo mongorestore -d vote --drop ../data/backup/vote

  reference:http://mongodbcanred.blogspot.tw/2015/01/mongodbmongodump-mongorestore.html

5.Use mongoDb by using python

   1)Install pymongo:
      import pymongo
      from pymongo import MongoClient
   2)Use monogoDb:
      client = MongoClient('localhost', 27017)
      db = client.{db name}
      collect = db[{collection name}]
      post1 = collect.find_one({"key": "value"})
      post1["key"]

6.MongoDB的基本操作

  • 與SQL的名詞對照

  • 與SQL指令的對照表

MongoDB

My SQL

新增

db.collection.find(條件)

select

修改

db.collection.insert(document)

insert

刪除

db.collection.update( criteria, objNew, upsert, multi )

update

查詢

db.collection.remove()

delete

  • 新增

  • 修改

  • 刪除

  • 查詢

    • find

      • db.collection.find(query, projection)

        • 定義: 從一個collection中找出documents,並回傳一個cursor物件

        • query:

            1. Query Selectors:

          • 2.Update Operators:

          • 3.Aggregation Pipeline Operators:

          • 4.Query Modifiers:

        • projection:

          • projection決定了哪些欄位要被回傳

        • cursor:

          • 一個指向a set of query的指標, 使用者可用迭代的方式從cursor取回查詢結果. 預設timeout為10分鐘

          • 取回方式

            • 1.手動迭代

              var myCursor = db.users.find( { type: 2 } );
              while (myCursor.hasNext(){
                print(tojson(myCursor.next()));
              }
              
              或是:
              
              var myCursor = db.users.find( { type: 2 } );
              while (myCursor.hasNext()) {
                 printjson(myCursor.next());
              }
              
              或是:
              
              var myCursor =  db.users.find( { type: 2 } );
              myCursor.forEach(printjson);
            • 2.轉為array

               var myCursor = db.inventory.find( { type: 2 } );
                var documentArray = myCursor.toArray();
                var myDocument = documentArray[3];
        • example:

          • 取出投過票的人

            collection.find({
              $or: [{
                  candidateId: {
                      $ne: 99
                  }
              }, {
                  candidateId2: {
                      $ne: 99
                  }
              }]
            }, {
              mail: 1
            }).toArray(function(err, items) {
                mongodb.close();
                  if (err) {
                    console.log("User.getVotedUsers:" + err);
                    callback(err);
                  }
                console.log(items);
                callback(null, items);
                //res.send(items);
            });
    • findOne

Previous1.2.5.4.Full joinNextChapter3: SQL Server

Last updated 5 years ago

Was this helpful?

reference:

https://docs.mongodb.com/v3.2/reference/operator/query/
https://docs.mongodb.com/v3.2/reference/operator/update/
https://docs.mongodb.com/v3.2/reference/operator/aggregation/
https://docs.mongodb.com/v3.2/reference/operator/query-modifier/
https://docs.mongodb.com/getting-started/shell/query/