Within a mongodb collection, I have documents that contain lists of embedded documents that look like this (using pymongo
with col
being an instance of pymongo.collection.Collection
):
In [1]: col.find_one({'reqid': 1})
Out[1]:
{u'_id': ObjectId('4fa3446f1d41c81bba000000'),
u'reports': [{u'comments': [u'A']},
{u'comments': [u'B']},
{u'comments': [u'A', u'B']},
{u'comments': [u'C']},
{u'comments': [u'A', u'B', u'C']}],
...} # Other fields
I now want with one $set
update to reset all comments
within a document to the empty list. I have tried the following but it doesn't work:
In [2]: col.update({'reqid': 1}, {'$set': {'reports.comments': []}})
In [3]: col.find_one({'reqid': 1}, {'reports.comments': 1})
Out[3]:
{u'_id': ObjectId('4fa3446f1d41c81bba000000'),
u'reports': [{u'comments': [u'A']},
{u'comments': [u'B']},
{u'comments': [u'A', u'B']},
{u'comments': [u'C']},
{u'comments': [u'A', u'B', u'C']}]}
I looked into the $ positional operator
for a possible solution but still can't figure it out. Its pretty straight forward to write the logic in python to do this but would definitely appreciate the elegance of doing this purely with a single pymongo.collection.Collection.update
call. Any help would be appreciated.