進入 REPL (Read-Evai-Print-Loop)互動命令列
>show dbs
> db
test #預設資料庫物件
> use mydatabase #使用mydatabase,沒有會自動建立
switched to db mydatabase
> db
mydatabase
插入一列資料,會自動建立資料表(集合) :class100,插入JSON檔案
>db.class100.insert({"stu":1,"name":"王小明","age":13})
WriteResult({ "nInserted" : 1 })
>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mydatabase 0.000GB
> show collections
class100
> db.class100.insert({"stu":2,"name":"林小美","age":13})
WriteResult({ "nInserted" : 1 })
> db.class100.find()
{ "_id" : ObjectId("5de6fef4ea717dbacb7553c6"), "stu" : 1, "name" : "王小明", "age" : 13 }
{ "_id" : ObjectId("5de6fff8ea717dbacb7553c7"), "stu" : 2, "name" : "林小美", "age" : 13 }
#_id為預設主鍵,會自動補上
> db.class100.find({"stu":1})
{ "_id" : ObjectId("5de6fef4ea717dbacb7553c6"), "stu" : 1, "name" : "王小明", "age" : 13 }
> db.class100.find({"name":"林小美"})
{ "_id" : ObjectId("5de6fff8ea717dbacb7553c7"), "stu" : 2, "name" : "林小美", "age" : 13 }