mercredi 2 octobre 2019

Generate random data and insert into mongodb using python

Have this python script that will generate random data

code :

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)

output :

data will print this

[{'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'}]

i need to send this data into mongo db using python (pymongo)

like this

db.collection.insert_many(data)

But there is error stating bulk write error and if i try to send a single json like this data[0] it is returning this document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping




Aucun commentaire:

Enregistrer un commentaire