pymongo基本功能不起作用

时间:2018-05-17 18:22:48

标签: mongodb pymongo

这是一个非常基本的mongodb客户端。

import pymongo

MONGO_DBNAME = 'db'
MONGO_HOST = '0.0.0.0'
MONGO_PORT = '5001'
MONGO_URI = 'mongodb://'+MONGO_HOST+':'+MONGO_PORT+'/'+MONGO_DBNAME

mongo = pymongo.MongoClient(MONGO_URI)
data = mongo.db['data']
data.insert_one({'data':'0.0'})

我收到错误: ServerSelectionTimeoutError: 0.0.0.0:5001: [WinError 10049] The requested address is not valid in its context

这只是最后一个命令搞砸了。我已经尝试使用数组insert_many()并得到相同的错误,但我没有看到有什么问题。 HALP?

编辑:是的我已尝试过其他解决方案,但我不认为它是一样的。 这是一个带有类似错误的烧瓶示例

# -*- coding: utf-8 -*-
from flask import Flask
from flask_pymongo import PyMongo
#import urllib
#from pprint import pprint

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'robo'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/robo'

mongo = PyMongo(app)

@app.route('/ya')
def ya():
    data = mongo.db.data
    data.insert_one({'test':'case'})
    return str(data)

@app.route('/uhhuh')
def uhhuh():
    data = mongo.db.data
    cursor = data.find({})
    x = []

    for doc in cursor:
        x.append(str(doc))

    return str(x)

if __name__ == '__main__':
    app.run()

-

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
    127.0.0.1 - - [17/May/2018 17:46:01] "GET /ya HTTP/1.1" 500 -

0 个答案:

没有答案