2013-05-05 72 views
1

我正在使用电机驱动程序连接到Mongo DB。 下面是插入数据的收集
电机返回无

client = motor.MotorClient('mongodb://localhost:27017').open_sync() 
    conn = client['database']['collection'] 
    result = conn.insert({'foo': 'bar'}) 
    print 'result:', result 

insert语句总是返回无代码。 这不是一个龙卷风应用程序。 马达只能与龙卷风一起使用吗? 如果不是为什么插入没有返回?

回答

0

我猜,WriteConcern没有从客户端驱动程序中设置。

如果将其设置为safe = true,那么您将获得插入操作的状态。否则,在安全= false的情况下,插入操作会触发并忘记。

你可以试试:

motor.MotorClient('mongodb://localhost:27017/?safe=true') 
+0

我试过这个选项,但没有帮助。 – 2013-05-05 05:18:39

+0

然后我猜,这不是在Motor drive客户端中指定WriteConcern的正确方法。 但是,您可以检查设置电机清理中的写入问题:http://motor.readthedocs.org/en/latest/api/motor_client.html#motor.MotorClient.write_concern – 2013-05-05 05:27:05

2

你用电机就像pymongo。但电机是异步的:这意味着当你的打印执行时,可能数据库请求还没有完成。 此外,电机插入不会返回任何内容,您需要使用回调函数作为第二个参数。参看the differences between pymongo and motorthe motor tutorial on how to insert a document

在你的情况,这soing的好办法是:

client = motor.MotorClient('mongodb://localhost:27017').open_sync() 
conn = client['database']['collection'] 
result = conn.insert({'foo': 'bar'}, callback=once_done) 

def once_done(result, error): 
    if error: print 'error:', error 
    else: 
     print 'result:', result