1. 程式人生 > >MongoDB Shell基本操作(一)

MongoDB Shell基本操作(一)

1、使用已經建立的admin管理賬戶新建超級使用者,方便後續演示操作;

[root@localhost ~]# /data2/mongodb-3.4.10/bin/mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
> use admin
switched to db admin
> db.auth("admin","admin")
1
> db.createUser({user:"root",pwd:"root",roles:["root"]})
Successfully added user: {
        "user" : "root",
        "roles" : [
                "root"
        ]
}
> show users
{
        "_id" : "admin.admin",
        "user" : "admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
{
        "_id" : "admin.root",
        "user" : "root",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

使用新建的root超級使用者登入

[root@localhost ~]# /data2/mongodb-3.4.10/bin/mongo -u root -p root admin
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017/admin
MongoDB server version: 3.4.10
> show dbs
admin  0.000GB
local  0.000GB

2、建立集合、插入資料 - insert

> use mydb
switched to db mydb
> user1 = {FName:"Test",LName:"User",Age:30,Gender:"M",Country:"US"}
{
        "FName" : "Test",
        "LName" : "User",
        "Age" : 30,
        "Gender" : "M",
        "Country" : "US"
}
> user2 = {Name:"Test USer",Age:45,Gender:"F",Country:"US"}
{ "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "US" }
> db.users.insert(user1)
WriteResult({ "nInserted" : 1 })
> db.users.insert(user2)
WriteResult({ "nInserted" : 1 })
> show dbs
admin  0.000GB
local  0.000GB
mydb   0.000GB
> show collections
users
> db.users.find()
{ "_id" : ObjectId("5afbd71d8dda484c0d9dae53"), "FName" : "Test", "LName" : "User", "Age" : 30, "Gender" : "M", "Country" : "US" }
{ "_id" : ObjectId("5afbd7228dda484c0d9dae54"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "US" }

也可以顯式建立集合,因為該集合已經存在了,所以報錯;

> db.createCollection("users")
{
        "ok" : 0,
        "errmsg" : "a collection 'mydb.users' already exists",
        "code" : 48,
        "codeName" : "NamespaceExists"
}

3、迴圈插入資料

> for(var i = 1; i <= 20; i++) db.users.insert({"Name":"Test User"+i,"Age":10+i,"Gender":"F","Country":"India"})
WriteResult({ "nInserted" : 1 })
> db.users.find()
{ "_id" : ObjectId("5afbd71d8dda484c0d9dae53"), "FName" : "Test", "LName" : "User", "Age" : 30, "Gender" : "M", "Country" : "US" }
{ "_id" : ObjectId("5afbd7228dda484c0d9dae54"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "US" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae55"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae56"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae57"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae58"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae59"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5a"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5b"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5c"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5d"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5e"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5f"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae60"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae61"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae62"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae63"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae64"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae65"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae66"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
Type "it" for more

4、顯式指定_id插入資料

必須保證_id的唯一性,否則插入失敗;

> db.users.insert({"_id":10,"Name":"explicit id"})
WriteResult({ "nInserted" : 1 })

5、更新資料 - $set

update()方法預設更新單個文件,如要更新所有符合條件的文件,通過設定multi選項為true來實現;

> db.users.update({"Gender":"F"}, {$set:{"Country":"UK"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({"Gender":"F"})
{ "_id" : ObjectId("5afbd7228dda484c0d9dae54"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae55"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae56"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae57"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae58"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae59"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5a"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5b"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5c"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5d"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5e"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5f"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae60"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae61"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae62"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae63"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae64"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae65"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae66"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae67"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
Type "it" for more
> db.users.update({"Gender":"F"}, {$set:{"Country":"UK"}}, {multi:true})
WriteResult({ "nMatched" : 21, "nUpserted" : 0, "nModified" : 20 })
> db.users.find({"Gender":"F"})
{ "_id" : ObjectId("5afbd7228dda484c0d9dae54"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae55"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae56"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae57"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae58"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae59"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5a"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5b"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5c"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5d"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5e"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5f"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae60"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae61"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae62"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae63"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae64"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae65"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae66"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae67"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "UK" }
Type "it" for more
> it
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae68"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "UK" }

6、新增欄位 - update() - $set

用$set使用一個欄位名稱,如果該欄位不存在,則該欄位會被新增到文件;

> db.users.update({},{$set:{"Company":"TestComp"}},{multi:true})
WriteResult({ "nMatched" : 23, "nUpserted" : 0, "nModified" : 23 })
> db.users.find()
{ "_id" : ObjectId("5afbd71d8dda484c0d9dae53"), "FName" : "Test", "LName" : "User", "Age" : 30, "Gender" : "M", "Country" : "US", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd7228dda484c0d9dae54"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae55"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae56"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae57"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae58"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae59"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5a"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5b"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5c"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5d"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5e"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5f"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae60"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae61"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae62"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae63"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae64"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae65"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae66"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "UK", "Company" : "TestComp" }
Type "it" for more

7、移除欄位 - update() - $unset

> db.users.update({},{$unset:{"Company":""}},{multi:true})
WriteResult({ "nMatched" : 23, "nUpserted" : 0, "nModified" : 23 })
> db.users.find()
{ "_id" : ObjectId("5afbd71d8dda484c0d9dae53"), "FName" : "Test", "LName" : "User", "Age" : 30, "Gender" : "M", "Country" : "US" }
{ "_id" : ObjectId("5afbd7228dda484c0d9dae54"), "Name" : "Test USer", "Age" : 45, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae55"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae56"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae57"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae58"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae59"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5a"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5b"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5c"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5d"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5e"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae5f"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae60"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae61"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae62"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae63"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae64"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae65"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("5afbd9d68dda484c0d9dae66"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "UK" }
Type "it" for more

8、刪除 - remove() - drop()

> db.users.remove({"Gender":"M"})
WriteResult({ "nRemoved" : 1 })
> db.users.find({"Gender":"M"})
> db.users.remove({})
WriteResult({ "nRemoved" : 22 })
> db.users.find()
> db.users.drop()
true
> show collections