具有此python脚本将生成随机数据
代码:
from faker import Faker
fake = Faker('en_US')
n = 3
id='123456'
data1 = []
for _ in range(n):
data= {
"id": "1ab",
"data": fake.text(),
"place": fake.state(),
"user_id": id
}
data1.append(data)
输出:
数据将打印此
[{'id': '1ab',
'data': 'Happen cut bank together whole still with. Health kind it relate would.\nWindow thus hair lawyer especially. Large ready husband religious body recent similar remember.',
'place': 'Minnesota',
'user_id': '123456'},
{'id': '1ab',
'data': 'Forget nature song hotel officer. Significant box first step someone.\nAudience take board service result energy energy. Him myself home your business option.',
'place': 'Georgia',
'user_id': '123456'},
{'id': '1ab',
'data': 'Show garden green. Or career today somebody very edge administration. Day myself reflect billion outside give price.\nBehavior poor learn health start. Challenge bill point lead lose tree.',
'place': 'Virginia',
'user_id': '123456'}]
我需要使用python(pymongo)将数据发送到mongo db]
喜欢这样
db.collection.insert_many(data)
但是出现错误,说明批量写入错误,如果我尝试发送像这样的单个json data[0]
,它将返回此错误document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
具有此python脚本将生成随机数据代码:从造假者导入Faker假= Faker('en_US')n = 3 id ='123456'data1 = []用于range(n)中的_:data = {“ id “:” ...
[当我准备此答案时,有人对此发表了评论,但为了确认,下面的代码应该可以工作。您需要在循环外插入insert_many(data1)以获得所需的结果。