mongo的基本操作
时间: 2023-12-07 17:37:12 浏览: 26
以下是MongoDB的基本操作示例:
1. 连接MongoDB数据库
```shell
mongo
```
2. 查看所有的数据库
```shell
show dbs
```
3. 切换到指定的数据库
```shell
use <database_name>
```
4. 查看当前所在的数据库
```shell
db
```
5. 查看当前数据库中的所有集合
```shell
show collections
```
6. 创建一个集合
```shell
db.createCollection("<collection_name>")
```
7. 向集合中插入一条数据
```shell
db.<collection_name>.insertOne({"name": "John", "age": 30})
```
8. 向集合中插入多条数据
```shell
db.<collection_name>.insertMany([
{"name": "John", "age": 30},
{"name": "Mike", "age": 25},
{"name": "Lucy", "age": 28}
])
```
9. 查询集合中的所有数据
```shell
db.<collection_name>.find()
```
10. 根据条件查询集合中的数据
```shell
db.<collection_name>.find({"age": {"$gt": 25}})
```
11. 更新集合中的数据
```shell
db.<collection_name>.updateOne({"name": "John"}, {"$set": {"age": 35}})
```
12. 删除集合中的数据
```shell
db.<collection_name>.deleteOne({"name": "John"})
```