2017-06-29 63 views
-1

给人属性的错误这是我的Python脚本:的Python脚本,而使用MongoDB的

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'没有属性'连接'

回答

0

我不知道你正在使用哪个版本的pymongo。但是基于latest document

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) 
+0

它给pymongo.errors.ServerSelectionTimeoutError的'的错误:本地主机:27017:[WinError 10061]无连接可以作出,因为目标机器积极地拒绝it' –

+0

这是因为你不在本地主机上运行MongoDB。 如果你有另一个Mongodb服务器,只需替换那些信息。 如果您需要在本地主机上运行MongoDB,请转至Mongodb的bin文件夹并运行mongod,并记住要创建数据文件夹(https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/#run -mongodb-community-edition) –

+0

您应该检查mongo服务器是否正常运行并验证端口号。 –