【问题标题】:how to use db.collection.createIndex method in flask pymnogo如何在烧瓶 pymnogo 中使用 db.collection.createIndex 方法
【发布时间】:2020-01-05 07:25:20
【问题描述】:

我想在我的 mongodb 文档中使用db.collection.createIndex 方法,但我得到了NameError: name 'db' is not defined。我如何在 flask_pymnogo 中使用这种方法? 请帮忙

这里是代码

@app.route('/insert')
def inserting_data():
    database = mongo.db.mylogin
    user_col = database.find({ "location": { "$nearSphere": { "$geometry": {"type": "Point", "coordinates": [21.0955, 81.0375]}, "$maxDistance":5000 } } })
    db.database.createIndex( { "location": "2dsphere" } )
    return list(user_col)

【问题讨论】:

    标签: python-3.x mongodb flask


    【解决方案1】:

    首先,您可能没有正确创建 db 对象来启动与数据库的连接。喜欢使用这些线条。

    client = MongoClient('localhost',27017)
    db = client.test_insert       ## name of the database
    collection = db.test_dataset  ## name of the collection
    

    另外,pymongo中创建索引的方法是create_index。你应该这样做

    my_collection.create_index(keys, session=None, **kwargs)
    

    您可以在这里查看documentation

    【讨论】:

    • 我想要flask_pymongo的这些配置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2012-06-16
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多