简体   繁体   中英

python join() list to string then giving me quotation marks in my mongoDB query

I'm working on making a bibliography with mongo, but it's an input website so if the user doesn't put in a certain field I need to account for this. It works well except I keep getting this error: bson.errors.InvalidDocument: cannot encode object: {"'author' : 'Fierri, Guy','chapter' : 'What he does cool','book' : 'The book','publisher' : 'Food Netword','year' : '2009 CE','pages' : '40-99'"}, of type: <class 'set'>

        query = []

        if author:
            word = "'author' : '" + str(author) + "'"
            query.append(word)
        if chapter:
            word = "'chapter' : '" + str(chapter) + "'"
            query.append(word)
        if book:
            word = "'book' : '" + str(book) + "'"
            query.append(word)
        if publisher:
            word = "'publisher' : '" + str(publisher) + "'"
            query.append(word)
        if year:
            word = "'year' : '" + str(year) + "'"
            query.append(word)
        if pages:
            word = "'pages' : '" + str(pages) + "'"
            query.append(word)

        query1 = ','.join(query)

        one = {"Name" : mashName}
        two = {"$push" : {"Sources" : { query1 }}}

        ##This is the code that works from the .py to update to mongo##
        print('db.collections.update_one({"Name" : "test_biliography"}, {"$push" : {"Sources" : {"author" : "Boy, Guy",  "chapter" : "What he does cool",  "book" : "The book",  "publisher" : "Food Network",  "year" : "2009 CE",  "pages" : "40-79"}}})')
        print("db.collections.update_one(" + str(one) + str(two))

        print(query1)

        db.collections.update_one(one, two)

The print output looks like:

db.collections.update_one({"Name" : "test_biliography"}, {"$push" : {"Sources" : {"author" : "Boy, Guy",  "chapter" : "What he does cool",  "book" : "The book",  "publisher" : "Food Network",  "year" : "2009 CE",  "pages" : "40-79"}}})
db.collections.update_one({'Name': 'Arabic_biliography'}{'$push': {'Sources': {"'author' : 'Fierri, Guy','chapter' : 'What he does cool','book' : 'The book','publisher' : 'Food Netword','year' : '2009 CE','pages' : '40-99'"}}}
'author' : 'Fierri, Guy','chapter' : 'What he does cool','book' : 'The book','publisher' : 'Food Netword','year' : '2009 CE','pages' : '40-99'

So the only difference I can find is the additional " around the information I'm trying push. any ideas on how I can get rid of these?

You should be building a dictionary based on the existence of certain values. What you're actually doing is constructing a string which then becomes the only element in a set whose value you assign to the "Sources" key.

You should probably be doing this:

query1 = {}

if author:
    query1['author'] = author
if chapter:
    query1['chapter'] = chapter
if book:
   query1['book'] = book
if publisher:
    query1['publisher'] = publisher
if year:
    query1['year'] = year
if pages:
    query1['pages'] = pages


one = {"Name": mashName}
two = {"$push": {"Sources": query1}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM