0

我想db.collection.createIndex在我的 mongodb 文档中使用方法,但我得到了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)
4

1 回答 1

0

首先,您可能没有正确创建 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)

您可以在此处查看文档

于 2020-01-06T02:01:17.310 回答