Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本在使用mongoDB时给出属性错误_Python_Mongodb - Fatal编程技术网

Python脚本在使用mongoDB时给出属性错误

Python脚本在使用mongoDB时给出属性错误,python,mongodb,Python,Mongodb,这是我的Python脚本: import pymongo from bson.objectid import ObjectId connection = pymongo.Connection(); db = connection["tutorial"]; employees = db["employees"]; employees.insert({"name": "Lucas Hightower", 'gender':'m', 'phone':'520-555-1212', 'age':

这是我的Python脚本:

import pymongo

from bson.objectid import ObjectId

connection = pymongo.Connection();

db = connection["tutorial"];
employees = db["employees"];

employees.insert({"name": "Lucas Hightower", 'gender':'m', 'phone':'520-555-1212', 'age':8});

cursor = db.employees.find();
for employee in db.employees.find():
    print(employee);

print(employees.find({"name":"Rick Hightower"})[0]);


cursor = employees.find({"age": {"$lt": 35}});
for employee in cursor:
     print("under 35: %s" % employee);


diana = employees.find_one({"_id":ObjectId("4f984cce72320612f8f432bb")});
print("Diana %s" % diana);hon Script:
当我执行以下python时,我得到以下错误:

我正在windows计算机上运行脚本

错误:connection=pymongo.connection();
AttributeError:模块“pymongo”没有属性“Connection”

我不知道您使用的是哪个版本的pymongo。但基于以下因素:


它给出的错误为
pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]无法建立连接,因为目标计算机主动拒绝它
这是因为您没有在本地主机上运行MongoDB。如果您有另一个Mongodb服务器,只需替换该信息。如果您需要在本地主机上运行MongoDB,请转到MongoDB的bin文件夹并运行mongod,记住创建数据文件夹()。您应该检查mongo服务器是否正常运行并验证端口号。您现在可以运行它吗,@AkshayShrivastav?不,它给出的错误仍然是服务器套接字无法连接到本地主机:27017
from pymongo import MongoClient

client = MongoClient("localhost", 27017)

db = client["tutorial"]
employees = db["employees"]

employees.insert({"name": "Lucas Hightower", 'gender':'m', 'phone':'520-555-1212', 'age':8})

cursor = employees.find()
for employee in cursor:
    print(employee)