1,基本操作
1.1切换数据库
test>use admin
switched to db admin
admin>
1.2建立用户
admin> db.createUser({user:"jerry", pwd:"123456", roles: ["root"]})
jerry:用户名
123456:密码
root:角色
{ ok: 1 }
1.3显示用户
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 进入数据库
use db_name
比如切换到mongo数据库,如果该数据库不存在,会自动创建
2.2 显示数据库
admin> db
admin
2.3删除数据库
admin> db.dropDatabase()
2.4 查看所有数据库
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查看数据库版本
admin> db.version()
7.0.14
3集合操作
3.1创建集合
admin> db.createCollection(name, options)
集合相当于普通数据库的表
一般不需要使用这个命令,因为在往集合里添加数据时,如果集合不存在会自动创建
options参数的可选值:
参数 | 说明 |
---|---|
capped | 是否固定集合大小,设置为true时,当集合元素达到设定的值时会覆盖最老的数据 |
size | 设置集合数据大小 |
max | 指定固定集合中最大文档数量 |
3.2删除集合
admin> db.collection_name.drop()
案例
admin>show tabless
person
user
admin> db.person.drop()
admin>show tabless
user
查看当前数据库所有集合
admin> show collections
system.users
system.version
admin>
4文档操作
4.1 添加操作
4.1.1 insert
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
admin>db.person.insertOne({"name": "Tom", age: 37});
{
acknowledged: true,
insertedId: ObjectId('66d7decaf5f38e175c5e739d')
}
4.1.3往集合里一次性添加多条数据 (insertMany )
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无条件查询
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带条件的查询
admin>db.person.find({"name": "Jerry"})
[ { _id: ObjectId('66d7de35f5f38e175c5e739c'), name: 'Jerry' } ]
5.3字段排序
升序
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 }
]
降序
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查询分页
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 }
]
等价于
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 }
]
跳过两条记录
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比较操作
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
}
]
5.6更多查询方式
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
这个命令的第三个参数是一个可选的选项
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)
admin>db.person.updateOne({_id: ObjectId("66d7df39f5f38e175c5e73a0")}, {$set: { age: 34}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
6.3更新多条数据(updateMany)
admin>db.person.updateMany(
{ name: "Cindy " }, // 查询条件
{ $set: { age: 32 } } // 更新操作
)
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}
6.4替换一条数据(replaceOne)
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)
admin> db.person.deleteOne({name: 'Jerry', age: 24})
{ acknowledged: true, deletedCount: 1 }
7.2删除多条(deleteMany)
7.2.1 无条件删除(deleteOne)
admin>db.collection.deleteMany()
7.2.2带条件删除(deleteOne)
admin>db.collection.deleteMany({name: "Jerry"})
8关闭MongoDB
8.1关闭方式一
bin/mongod --port=27017 --dbpath=/etc/mongodb/data --shutdown
8.2关闭方式二
use admin
db.shutdownServer()
8.3关闭方式三
通过kill进程来关闭
ps -ef | grep mongodb
kill PID