1,基本操作
1.1切换数据库
代码语言:javascript复制test>use admin
switched to db admin
admin>
1.2建立用户
代码语言:javascript复制admin> db.createUser({user:"jerry", pwd:"123456", roles: ["root"]})
jerry:用户名
123456:密码
root:角色
{ ok: 1 }
1.3显示用户
代码语言:javascript复制admin> show users
[
{
_id: 'admin.jerry',
userId: UUID('a86e9f3d-598e-4cbf-9419-4fa459f6773b'),
user: 'jerry',
db: 'admin',
roles: [ { role: 'root', db: 'admin' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
]
2数据库操作
2.1 进入数据库
代码语言:javascript复制use db_name
比如切换到mongo数据库,如果该数据库不存在,会自动创建
2.2 显示数据库
代码语言:javascript复制admin> db
admin
2.3删除数据库
代码语言:javascript复制admin> db.dropDatabase()
2.4 查看所有数据库
代码语言:javascript复制admin> show dbs
admin 132.00 KiB
config 92.00 KiB
local 40.00 KiB
admin> show databases
admin 132.00 KiB
config 92.00 KiB
local 40.00 KiB
2.5查看数据库版本
代码语言:javascript复制admin> db.version()
7.0.14
3集合操作
3.1创建集合
代码语言:javascript复制admin> db.createCollection(name, options)
集合相当于普通数据库的表
一般不需要使用这个命令,因为在往集合里添加数据时,如果集合不存在会自动创建
options参数的可选值:
参数 | 说明 |
---|---|
capped | 是否固定集合大小,设置为true时,当集合元素达到设定的值时会覆盖最老的数据 |
size | 设置集合数据大小 |
max | 指定固定集合中最大文档数量 |
3.2删除集合
代码语言:javascript复制admin> db.collection_name.drop()
案例
代码语言:javascript复制admin>show tabless
person
user
admin> db.person.drop()
admin>show tabless
user
查看当前数据库所有集合
代码语言:javascript复制admin> show collections
system.users
system.version
admin>
4文档操作
4.1 添加操作
4.1.1 insert
代码语言:javascript复制admin>db.person.insert({"name": "Jerry"});
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
acknowledged: true,
insertedIds: { '0': ObjectId('66d7de35f5f38e175c5e739c') }
}
admin>
4.1.2 insertOne
往集合里添加一条数据,同insert
代码语言:javascript复制admin>db.person.insertOne({"name": "Tom", age: 37});
{
acknowledged: true,
insertedId: ObjectId('66d7decaf5f38e175c5e739d')
}
4.1.3往集合里一次性添加多条数据 (insertMany )
代码语言:javascript复制admin>db.person.insertMany([{"name": "Cindy", age: 43}, {"name": "Linda", age: 21}, {"name": "Jessca", age: 30}]);
{
acknowledged: true,
insertedIds: {
'0': ObjectId('66d7df39f5f38e175c5e739e'),
'1': ObjectId('66d7df39f5f38e175c5e739f'),
'2': ObjectId('66d7df39f5f38e175c5e73a0')
}
}
5查询操作
5.1无条件查询
代码语言:javascript复制admin>db.person.find()
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 30
}
]
5.2带条件的查询
代码语言:javascript复制admin>db.person.find({"name": "Jerry"})
[ { _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' } ]
5.3字段排序
升序
代码语言:javascript复制admin>db.person.find().sort({age: 1});
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' },
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 30
},
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 }
]
降序
代码语言:javascript复制admin>db.person.find().sort({age: -1});
[
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 30
},
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' }
]
5.4查询分页
代码语言:javascript复制admin>db.person.find().skip(0).limit(3)
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 }
]
等价于
代码语言:javascript复制admin>db.person.find().limit(3)
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 }
]
跳过两条记录
代码语言:javascript复制admin> db.person.find().skip(2);
[
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 30
}
]
5.5比较操作
代码语言:javascript复制admin> db.person.find({age: {$gte:30 }})
[
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 34
}
]
- $gte:大于等于(Greater Than or Equal to)
- $lt:小于(Less Than)
- $lte:小于等于(Less Than or Equal to)
- $gt:大于(Greater Than)
5.6更多查询方式
代码语言:javascript复制admin> db.person.find({name: {$eq: "Jerry"}})
admin> db.person.find({name: {$eq: "Jerry"}})
[ { _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' } ]
admin> db.person.find({name: {$eq: "jerry"}})
admin>
说明:大小写敏感
6更新操作
6.1 update
这个命令的第三个参数是一个可选的选项
- upsert:如果不存在update的记录,是否插入新的记录。默认false,不插入
- multi:是否按条件查询出的多条记录全部更新。默认false,只更新找到的第一条记录
- writeConcern:决定一个写操作落到多少个节点上才算成功。
admin>db.person.find()
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{_id: ObjectId('66d7df39f5f38e175c5e73a0'),name: 'Jessca',age: 30
}
]
admin> db.person.update({name: 'Jerry'},{$set:{name: 'Jerry',age: 28}}, {multi:true})
DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite.
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
admin>db.person.find()
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry', age: 28 },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 30
}
]
6.2更新单条数据(updateOne)
代码语言:javascript复制admin>db.person.updateOne({_id: ObjectId("66d7df39f5f38e175c5e73a0")}, {$set: { age: 34}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
6.3更新多条数据(updateMany)
代码语言:javascript复制admin>db.person.updateMany(
{ name: "Cindy " }, // 查询条件
{ $set: { age: 32 } } // 更新操作
)
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}
6.4替换一条数据(replaceOne)
代码语言:javascript复制replaceOne 会用新的文档完全取代旧的文档,包括 _id 字段。如果新的文档中没有 _id 字段或 _id 字段值与旧的不同,会导致文档被赋予新的 _id。
admin>db.person.replaceOne(
{ "name": " Jerry " }, // 过滤条件
{ "_id": ObjectId('66d7de35f5f38e175c5e739d'), "name": "Jerry", "age": 24}, // 新的文档
{ upsert: true } // 使用 upsert 选项
);
{
acknowledged: true,
insertedId: '66d7de35f5f38e175c5e739c',
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 1
}
admin> db.person.find()
[
{ _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry', age: 28 },
{ _id: ObjectId('66d7decaf5f38e175c5e739d'), name: 'Tom', age: 37 },
{ _id: ObjectId('66d7df39f5f38e175c5e739e'), name: 'Cindy', age: 43 },
{ _id: ObjectId('66d7df39f5f38e175c5e739f'), name: 'Linda', age: 21 },
{
_id: ObjectId('66d7df39f5f38e175c5e73a0'),
name: 'Jessca',
age: 34
},
{ _id: ObjectId('66d7de35f5f38e175c5e739d'), name: 'Jerry', age: 24 }
]
7删除操作
7.1删除一条记录(deleteOne)
代码语言:javascript复制admin> db.person.deleteOne({name: 'Jerry', age: 24})
{ acknowledged: true, deletedCount: 1 }
7.2删除多条(deleteMany)
7.2.1 无条件删除(deleteOne)
代码语言:javascript复制admin>db.collection.deleteMany()
7.2.2带条件删除(deleteOne)
代码语言:javascript复制admin>db.collection.deleteMany({name: "Jerry"})
8关闭MongoDB
8.1关闭方式一
代码语言:javascript复制bin/mongod --port=27017 --dbpath=/etc/mongodb/data --shutdown
8.2关闭方式二
代码语言:javascript复制use admin
db.shutdownServer()
8.3关闭方式三
通过kill进程来关闭
代码语言:javascript复制ps -ef | grep mongodb
kill PID