mongodb 插入数据
时间: 2023-11-17 20:57:16 浏览: 99
好的,关于 MongoDB 插入数据,你需要先连接到 MongoDB 数据库,然后选择要插入数据的集合,最后使用 `insert_one()` 或 `insert_many()` 方法插入数据。具体操作如下:
```python
import pymongo
# 连接到 MongoDB 数据库
client = pymongo.MongoClient("mongodb://localhost:27017/")
# 选择要插入数据的集合
db = client["mydatabase"]
collection = db["mycollection"]
# 插入单条数据
data = {"name": "John", "address": "Highway 37"}
collection.insert_one(data)
# 插入多条数据
data_list = [
{"name": "Amy", "address": "Apple st 652"},
{"name": "Hannah", "address": "Mountain 21"},
{"name": "Michael", "address": "Valley 345"},
{"name": "Sandy", "address": "Ocean blvd 2"},
{"name": "Betty", "address": "Green Grass 1"},
{"name": "Richard", "address": "Sky st 331"},
{"name": "Susan", "address": "One way 98"},
{"name": "Vicky", "address": "Yellow Garden 2"},
{"name": "Ben", "address": "Park Lane 38"},
{"name": "William", "address": "Central st 954"},
{"name": "Chuck", "address": "Main Road 989"},
{"name": "Viola", "address": "Sideway 1633"}
]
collection.insert_many(data_list)
```
阅读全文