我试图使用以下代码获取mongo数据库中存在的一些ID:
client = MongoClient('xx.xx.xx.xx', xxx)
db = client.test_database
db = client['...']
collection = db.test_collection
collection = db["..."]
for cursor in collection.find({ "$and" : [{ "followers" : { "$gt" : 2000 } }, { "followers" : { "$lt" : 3000 } }, { "list_followers" : { "$exists" : False } }] }):
print cursor['screenname']
print cursor['_id']['uid']
id = cursor['_id']['uid']
Run Code Online (Sandbox Code Playgroud)
但是,过了一会儿,我收到这个错误:
pymongo.errors.CursorNotFound:游标id'...'在服务器上无效.
我发现这篇文章提到了这个问题.然而,我不清楚采取哪种解决方案.可以使用find().batch_size(30)吗?上面的命令到底是做什么的?我可以使用所有数据库ID batch_size吗?
我需要sid在大约500K文档的集合中为每个文档创建一个新字段.每个sid都是独一无二的,并基于该记录的现有roundedDate和stream字段.
我正在使用以下代码:
var cursor = db.getCollection('snapshots').find();
var iterated = 0;
var updated = 0;
while (cursor.hasNext()) {
var doc = cursor.next();
if (doc.stream && doc.roundedDate && !doc.sid) {
db.getCollection('snapshots').update({ "_id": doc['_id'] }, {
$set: {
sid: doc.stream.valueOf() + '-' + doc.roundedDate,
}
});
updated++;
}
iterated++;
};
print('total ' + cursor.count() + ' iterated through ' + iterated + ' updated ' + updated);
Run Code Online (Sandbox Code Playgroud)
它起初效果很好,但几个小时后大约有100K记录,它出错了:
Error: getMore command failed: {
"ok" : …Run Code Online (Sandbox Code Playgroud) 我正在使用flask-mongoengine 扩展,我有一个像这样的用户类:
class User(db.Document, UserMixin):
email = db.StringField(max_length=120, required=True, unique=True)
password_hash = db.StringField(max_length=80, required=True)
active = db.BooleanField()
fb_id = db.StringField(max_length=120, required=False)
def __init__(self, email, password, fb_id=None, active=True):
hashp = md5.md5(password).hexdigest()
self.email=email
self.password_hash=hashp
self.fb_id=fb_id
self.active=active
Run Code Online (Sandbox Code Playgroud)
但是当我做一个简单的获取时:
User.objects.get(email = email)
我收到错误:
TypeError: __init__() got an unexpected keyword argument 'password_hash'
但是我在我的 init 中设置了 password_hash。令人惊讶的是,如果我删除整个__init__并通过 args 传递所有内容,它工作正常。
所以我认为我在__init__. 我很坚持这一点,将不胜感激您的帮助。