我想在我的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)发布于 2020-01-06 02:01:17
首先,您可能没有正确地创建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)你可以看看这里的文档。
https://stackoverflow.com/questions/59597828
复制相似问题