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