【发布时间】:2014-11-02 10:49:51
【问题描述】:
我已阅读有关切换集合以保存文档的 mongoengine 文档。并测试此代码并成功运行:
from mongoengine.context_managers import switch_db
class Group(Document):
name = StringField()
Group(name="test").save() # Saves in the default db
with switch_collection(Group, 'group2000') as Group:
Group(name="hello Group 2000 collection!").save() # Saves in group2000 collection
但问题是当我想在开关集合中找到保存的文档时,switch_collection 根本不起作用。
with switch_collection(Group, 'group2000') as GroupT:
GroupT.objects.get(name="hello Group 2000 collection!") # Finds in group2000 collection
【问题讨论】:
-
您可以尝试指定您要从中查询该记录的数据库吗? GroupT.objects.using(dbname).filter(name="hello Group 2000 collection!")
-
@SyedMauzeRehan 和其他收藏一样。
标签: python mongodb python-2.7 mongoengine