mongoDB 是一種 free schema ,所以就算沒有 database ,只要建立 collection 他會幫你建立 database

database 操作
 use dbName — connect and create database (但不會在 db list 顯示,至少要 insert 一個collection)
 show dbs — show db list
 dbs — show current db
 db.dropDatabase() — delete database

collection 操作

db.createCollection(name, options)
 options is a object.

db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )

show collections

document 操作

 document is a object
 如果沒有在 document 裡面放 _id, 系統會預設幫你加一個 _id,設定如下
 _id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows −

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: '',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100

title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
by: 'tutorials point',
url: '',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0

To insert the document you can use also. If you don’t specify _id in the document then save() method will work same as insert() method. If you specify _id then it will replace whole data of document containing _id as specified in save() method.

 db.collectionName.find(Operation) 如果沒有 operation 就show all data in this colleciton
 ex.{address: 'Taiwan'})

// 如果想要以多個key 搜尋,則加一個 $and key,value 塞 array 進去
db.mycol.find({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]})

// 只顯示特定欄位的方式{}, {selectKey1: 1, selectKey2: 1, selectKey3: 0})

// 限制顯示資料的筆數

// 美化
db.collectionName.find().pretty() 美化

// 按照欄位排序
db.COLLECTION_NAME.find().sort({KEY:1}) -- 1 代表 升冪,0 代表不用, -1 代表 降冪


 db.collectionName.update(SELECTION_CRITERIA, UPDATED_DATA)
 SELECTION_CRITERIA: is a key:value object, which you want to change the value
 UPDATED_DATA: is the key:value which involve by $set key
 ex.{name: 'test1'},{$set:{name: 'test4'}})


db.mycol.remove({'title':'MongoDB Overview'})
Show your support

Clapping shows how much you appreciated Zuray’s story.