Jota
Jota

Reputation: 127

TypeError: documents must be a non-empty list

I'm doing a program using Twitter API and MongoDB in 2.7 Python language.

I get a timeline and put it in a dictionary, which I want to store in a MongoDB database. To do this I have next code:

def saveOnBD(self, dic):
    client = MongoClient("xxxx", "port")
    db = client.DB_Tweets_User_Date
    collection = db.tweets
    collection.insert_many(dic)

I'm debbuging and dic it's not empty but I get next error:

TypeError: documents must be a non-empty list

How can I fix it?

Upvotes: 1

Views: 15344

Answers (4)

uma mahesh
uma mahesh

Reputation: 124

Don't use insertmany if list size is zero

Upvotes: 0

Panti Andrei
Panti Andrei

Reputation: 21

A list of documents must be passed to insert_many method

E.g.:

collection.insert_many([dic])

Upvotes: 1

Prakruti Chandak
Prakruti Chandak

Reputation: 1

you can either put in an entry before running the bulk entry function or use insert()

Upvotes: 0

Jota
Jota

Reputation: 127

I trying many options, but i solved that question changing the post method.

Instead of:

collection.insert_many(dic)

I used this:

collection.insert_one(dic)

I supose that, as I try to post only a variable(dic) and "insert_many()" is for many variables that retun me the error. That change solved me the question

Upvotes: 1

Related Questions