我注意到了pymongo库的奇怪行为。 当我从单元测试中运行以下代码时,一切正常(2个文档存储在他们的集合中并且_id已设置)。
property_db_item = {#"_id": str(bson.ObjectId()),"url": item['url'],
"price": item['price'],"rooms": item['rooms'],
"area": item['area'],"flor": item['flor'],
"street": item['street'],"city": item['city'],
"region": item['region'],"country": item['country'],
"photoUrls": item['photoUrls'],"updatedAt": str(date)}
db.properties.update({"url": item['url']}, property_db_item, upsert=True)
db.property_trends.update(
{"url": item['url'], "date": date.strftime("%Y-%m")},
{
"$setOnInsert": {"url": item['url'], #"_id": str(bson.ObjectId())],
"date": date.strftime("%Y-%m")},
"$addToSet": {"prices": {date.strftime("%Y-%m-%d"): item['price']}}
}, upsert=True)`
但是当我从终端执行它时(scrapy crawl myParser),并收到此错误:
DuplicateKeyError(细节[ “ERR”]) pymongo.errors.DuplicateKeyError:E11000重复键错误索引:local.properties。$ id dup key:{:null}
答案 0 :(得分:2)
这是现有的bug。 Up local
数据库失败
E11000 duplicate key error index: local.properties.$_id_ dup key: { : null }
例外。
以下是一个例子:
> use local
switched to db local
> db.properties.update({'url': 'test1'}, {'url2': 'test2'}, upsert=true)
> db.properties.update({'url': 'test1'}, {'url2': 'test2'}, upsert=true)
E11000 duplicate key error index: local.properties.$_id_ dup key: { : null }
> use local2
switched to db local2
> db.properties.update({'url': 'test1'}, {'url2': 'test2'}, upsert=true)
> db.properties.update({'url': 'test1'}, {'url2': 'test2'}, upsert=true)