Append new value to a list in PyMongo
NickName:bunbohue Ask DateTime:2016-11-21T00:46:22

Append new value to a list in PyMongo

I have a collection in MongoDB that I store on mlab like this

{
   'course_id': '...',
   'course_description: '...',
   'reviews:' [
      {
         'hours': ...
         'review': ...
      },
      {
         'hours': ...
         'review': ...
      }
    ]
}

I'm having trouble appending new values to the list_to_append above. None of these methods below works for me. I'm using PyMongo (version 2.8) to interact with MongoDB.

Here is the code snippet that I'm using to update the list

@app.route('/', methods=['GET', 'POST'])
def index():
    search_form = SearchBar()
    if request.method == 'GET':
        review_dict = {'hours': "5 hours", 'review': "Something something"}

        # Test with one collection and one course first
        mongo.db['CS'].update({'course_id:': 'CS 101'}, {'$push': {'reviews:': review_dict}})
        return render_template('index.html', form=search_form)

I've tried these methods below but they didn't work for me.

Append item to MongoDB document array in PyMongo without re-insertion

How to insert an element to MongoDB internal list?

inserting items in list in mongodb document

Copyright Notice:Content Author:「bunbohue」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40706645/append-new-value-to-a-list-in-pymongo

More about “Append new value to a list in PyMongo” related questions

Append new value to a list in PyMongo

I have a collection in MongoDB that I store on mlab like this { 'course_id': '...', 'course_description: '...', 'reviews:' [ { 'hours': ...

Show Detail

How pymongo define the key for append the value

Mongodb collections like this: `col={"a":{"x":1,"y":2},"b":[{"w":5,"z":7},{"w":8,"z":5}]}` It wor

Show Detail

Pymongo find using $in with a list

The folowing expression works fine, returning the values that match the values list: [...].find({"FieldName":{"$in":["Value1", "Value2", "Value3"]}}) But i have the list of values in a li

Show Detail

pymongo - how to append values to array?

I am trying to create a database if it is not there already in mongo, and then create a collection in the database; then insert/update a document (which contains two keys, called erp and dataset wh...

Show Detail

update value in collection list item in pymongo

I'm a beginner in pymongo and I have a users collection that stores a user and a comments list An instance of a user is like user = {"Name":"Bill" , "Comments":["nice" , "bad"]} &#x

Show Detail

Ansible: How to append string to each value of list

I'm trying to append a string to each value of a list in ansible, so basically am trying to install multiple pip modules offline using .whl files. I have two files in /opt/tmp/ path vars: DIR: /op...

Show Detail

mongdb pymongo disappearing list of items from collection

So I run a local mongodb by running $ mongod from the terminal. I then connect to it and create a small database with a python script using pymongo : import random import string import pymongo c...

Show Detail

comprehension list in pymongo query

Good morning everyone, I would like to write a pymongo snippet to query from a database all documents having a specific field value within a given list (so also a value which is a subset of any ele...

Show Detail

Append item to MongoDB document array in PyMongo without re-insertion

I am using MongoDB as the back-end database for Python web application (PyMongo + Bottle). Users can upload files and optionally 'tag' these files during upload. The tags are stored as a list withi...

Show Detail

Creating a list in PyMongo

How does one create a list in PyMongo? C = pymongo.MongoClient("localhost",27017) db = C.coll.savee Val= 12345 db.insert_one(["LIST",Val])

Show Detail