我正在使用马达驱动程序连接Mongo DB。下面是向集合中插入数据的代码
client = motor.MotorClient('mongodb://localhost:27017').open_sync()
conn = client['database']['collection']
result = conn.insert({'foo': 'bar'})
print 'result:', resultinsert语句始终返回None。这不是Tornado应用程序。马达只能与龙卷风一起使用吗?如果不是,为什么插入返回none?
发布于 2013-05-05 23:49:13
你就像使用pymongo一样使用引擎。但是马达是异步的:这意味着当你的print被执行时,db请求可能还没有完成。此外,电机插入不返回任何内容,您需要使用回调函数作为其第二个参数。请参阅the differences between pymongo and motor和the motor tutorial on how to insert a document。
在您的情况下,解决此问题的好方法是:
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发布于 2013-05-05 13:16:08
我猜,WriteConcern不是从客户机驱动程序中设置的。
如果您将其设置为safe=true,那么您将获得插入操作的状态。否则,使用safe=false时,插入操作将被触发并忘记。
您可以尝试:
motor.MotorClient('mongodb://localhost:27017/?safe=true')https://stackoverflow.com/questions/16381549
复制相似问题