1

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

Community
  • 1
  • 1
bunbohue
  • 63
  • 6
  • What specific problem(s) are you having? Why don't the solutions to those questions work for you? – dirn Nov 20 '16 at 16:53
  • I'm not sure why it's not working. I'm sure I'm using the right collection to update. The new element/value did not seem to be appended to the list on mlab. – bunbohue Nov 20 '16 at 17:02
  • At this point you'll need to [edit](http://stackoverflow.com/posts/40706645/edit) your question to include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Without seeing what you're doing, it's hard to tell you why it isn't working. – dirn Nov 20 '16 at 17:03
  • Thanks for the suggestion. – bunbohue Nov 20 '16 at 17:11

0 Answers0