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

猜你喜欢

转载自blog.csdn.net/hellboy0621/article/details/80336888