【问题标题】:Run string command in pymongo在 pymongo 中运行字符串命令
【发布时间】:2018-03-13 09:57:13
【问题描述】:

我有以下字符串命令

query = "{},{'_id': False}"

现在我想在下面的代码中运行上面的命令

client = MongoClient('mongodb://'+host+':'+port+'/')
db_col = database.split(',')
database, collection = db_col[0].strip(), db_col[1].strip()
collection = client[database][collection]
collection = collection.find(query).limit(10)

有什么解决办法吗

【问题讨论】:

    标签: django mongodb python-2.7 pymongo


    【解决方案1】:

    我找到了解决上述问题的方法

    我将完整的查询转换成字符串并使用 python eval 函数在 python 服务器上执行。

    见以下代码

    这是我的字符串查询

    query = "collection.find({},{'_id': False}).limit(10)"
    

    这就是我在python中所做的

    collection_cursor = eval(query)
    

    【讨论】:

    猜你喜欢
    • 2014-07-10
    • 2021-02-20
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多