从MongoEngine的文档中,我发现用户可以设置首选collection name
class Users(Document):
user_id = SequenceField(primary_key=True)
meta = {'collection': "test_collection"}
有没有一种方法可以让我稍后通过发送查询来获取此集合名称(此处test\u collection?我的目的是在检查collection
的存在后do something
。
我发现的解决方案是,在得到db name
和list of collections
检查存在性之后,但这里的限制是我必须自己编写所需的collection name
db = get_db() # This one from connect parameter get the set database name
col = db.list_collection_names() # get all collections name of used database from the previous line
print("existance checking: ", "test_collection" in db.list_collection_names()) # here I don't want to hard coded the collection name "test_collection"
我尝试了以下操作,但出现错误
for x in Users.objects:
print("meta: ", x.meta)
错误消息:
AttributeError: 'Users' object has no attribute 'meta'
尝试前一种方法的原因,因为我从中得到了有效的结果
for x in Users.objects:
print("user_id: ", x.user_id)
我是MongoDB的新手,所以不知道它是如何工作的,也不知道它是否可能。因此,如果有任何查询来获取PyMongo/MongoEngine在Users
类中指定的当前集合名称,将非常有用
目前没有回答
相关问题 更多 >
编程相关推荐