Adam Van Oijen
Adam Van Oijen

Reputation: 545

TypeError: insert() takes at least 2 arguments (1 given) mongoDB

sorry if this is dumb question, but this is the first time i've used python and Mongo DB. Anyway, the problem I have is that I am trying to insert() a string to be stored in my data base -by read()-ing data in with a loop and then giving insert() line 1 and line2 in a single string (This is probably a messy way of doing it but I don't know to make read() read 2 lines at a time.)- but I get this error when running it: TypeError: insert() takes at least 2 arguments (1 given)

from pymongo import MongoClient

client = MongoClient("192.168.1.82", 27017)
db = client.local
collection = db.JsonDat

file_object = open('a.txt', 'r')

post=collection.insert()

readingData = True
def readData():
    while readingData==True:
        line1 = file_object.readline()
        line2 = file_object.readline()

        line1
        line2

        if line1 or line2 == "":
            readingData = False

        dbData = line1 %line2
        post(dbData)
    print collection.find()

Upvotes: 0

Views: 11690

Answers (2)

Neil Lunn
Neil Lunn

Reputation: 151092

It appears as has been pointing out that you are attempting to create a closure on the insert method. In this case I don't see the point as it will never be passed anywhere and/or need to reference something in the scope outside of where it was used. Just to the insert with arguments in the place where you actually want to use it, ie where you are calling post with arguments.

Happy to vote up the answer already received as correct. But really want to point out here that you appear to be accessing the local database. It is likely found this by inspecting your new mongo installation and saw this in the databases list.

MongoDB creates databases and collections automatically as you reference them and commit your first insert.

I cannot emphasize enough DO NOT USE THE LOCAL DATABASE. It is for internal use only and will only result in you posting more issues here from the resulting problems.

Upvotes: 1

Cairnarvon
Cairnarvon

Reputation: 27752

The relevant part of your code is this:

post=collection.insert()

What you're doing there is calling the insert method without arguments rather than assigning that method to post. As the insert method takes as its arguments at least the document you're trying to insert and you haven't passed it anything, it only receives (implicitly) the object itself; that is, one argument instead of the at-least-two it expects.

Removing the parentheses ought to work.

Upvotes: 4

Related Questions