Python Forum
insert more data at once in MongoDB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
insert more data at once in MongoDB
#1
Hello Coders!

I'm trying to upload data with Python to MongoDB from a form in HTML.

I can do it with no problem if I upload only the form, but I don't know how to add additional values to the same submission, I get errors. I show you my code:

@app.route('/insert_recipe', methods=['POST'])
def insert_recipe():
    recipes = mongo.db.recipes
    
    x = datetime.datetime.now()
    posted = x.strftime("%d") + " " + x.strftime("%b")  + " " + x.strftime("%Y")
    
    
    date_post = {
        "posted" : posted
    }
    

    recipes.insert_one(request.form.to_dict(), posted)


    
    if request.files:

        image = request.files["picture"]

        path = os.getcwd()
        image_dir = path + "/static/images/" + image.filename
        
        image.save(image_dir)

    # Once that's done, we redirect to recipe.html, so we can view the new recipe in our collection.
    return redirect(url_for('index'))
As you can see request.form.to_dict() is all the data coming from the form, and posted is an additional value that I would like to add. It's important for me to know how to do it, but this doesn't work, I can only manage to upload the form if I leave the code simply with:

    recipes = mongo.db.recipes
    recipes.insert_one(request.form.to_dict())
Second thing I would like to rename the image with the Object ID that the submission creates, is this possible?

Thank you very much! I'm a beginner then forgive my stupid question.

Thank you!
Reply
#2
Why don't you combine the two dictionaries? You might want to look at the update method of the dict class if you want to use something built in (the third party library Toolz has a function called merge also).
Reply
#3
Thank you very much, I solved adding a value to the dictionary:

    dict1 = request.form.to_dict()
    dict1['posted'] = posted

    recipes.insert_one(dict1)
Now I just need to understand how to get the objectID and rename the picture with the objectID value.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  swapping data in mongodb saisankalpj 0 730 Aug-30-2022, 12:42 PM
Last Post: saisankalpj
  How to insert different types of data into a function DrData82 0 1,249 Feb-10-2022, 10:41 PM
Last Post: DrData82
  How to read rainfall time series and insert missing data points MadsM 4 2,168 Jan-06-2022, 10:39 AM
Last Post: amdi40
Exclamation MongoDB cannot connect - pymongo speedev 1 2,087 Aug-21-2021, 01:35 PM
Last Post: ndc85430
Bug Djongo (mongoDb) - Django rest framework Problem with search on JsonFields grzybekrafal 0 2,467 May-11-2021, 09:04 AM
Last Post: grzybekrafal
  Retrieve data from MongoDB dsuk 2 2,288 Aug-24-2020, 09:22 PM
Last Post: micseydel
  find a string in a field in MongoDB Leon79 2 2,418 Jul-19-2020, 09:20 PM
Last Post: menator01
  Insert data into sql after joining two excel data from python NeerajYadav 2 2,063 Jan-29-2020, 02:29 PM
Last Post: parthi1705
  How to insert data if not exists in mysql? farah97 0 2,822 Dec-29-2019, 08:32 AM
Last Post: farah97
  Load JSON file data into mongodb using pymongo klllmmm 1 11,861 Jun-28-2019, 12:47 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020