def update_many(cls, documents, *fields):
"""
Update multiple documents. Optionally a specific list of fields to
update can be specified.
"""
from mongoframes.queries import to_refs
# Ensure all documents have been converted to frames
frames = cls._ensure_frames(documents)
all_count = len(documents)
assert len([f for f in frames if '_id' in f._document]) == all_count, \
"Can't update documents without `_id`s"
# Send update signal
signal('update').send(cls, frames=frames)
# Prepare the documents to be updated
# Check for selective updates
if len(fields) > 0:
documents = []
for frame in frames:
document = {'_id': frame._id}
for field in fields:
document[field] = cls._path_to_value(
field,
frame._document
)
documents.append(to_refs(document))
else:
documents = [to_refs(f._document) for f in frames]
# Update the documents
for document in documents:
_id = document.pop('_id')
cls.get_collection().update(
{'_id': _id}, {'$set': document})
# Send updated signal
signal('updated').send(cls.__class__, frames=frames)
评论列表
文章目录