在db中放入mongodb的数据已经存在

时间:2017-09-15 11:57:51

标签: python mongodb scrapy pymongo

我和你一起去,因为在我的数据库中我尝试将scage抓取的数据用了三天,但它没有用。

我尝试填充我的数据库中的标签区域(名称是mtdbdd),里面有自动增量ID(我想也许这是我问题的原因但我不喜欢'真的知道)。所以在我的scrapy项目中,我没有文件名id,也许我应该有。

我尝试这样的事情:

def put(self, item):
    MONGO_URI = 'mongodb://root:root@127.0.0.1:8889'

    client = MongoClient(MONGO_URI)
    db = client.mtdbdd
    coll = db.land

    try:
        result = coll.insert_one(item)
    except coll.errors.OperationFailure as e:
        print e.code
        print e.details

我有错误:

KeyError: 'Annonce does not support field: _id'

(Annonce是我项目的名称)

如果有人可以帮助我,那就太棒了! 谢谢你们!

编辑:我的项目:

class Annonce(Item):
    url = Field()
    title = Field()
    price = Field()
    surface = Field()
    description = Field()
    city = Field()
    zipcode = Field()
    date = Field()
    img = Field()
    scrap = Field()
    url_title = Field()
    full_address = Field()
    viability = Field()
    gas = Field()
    visiblePhone = Field()
    isolate = Field()
    slope = Field()
    beingChanged = Field()
    toRenew = Field()
    csvFile = Field()
    views = Field()
    deleted = Field()

1 个答案:

答案 0 :(得分:0)

item转换为dict,然后将其插入数据库,即更改

result = coll.insert_one(item)

result = coll.insert_one(dict(item))

作为旁注,MongoDB没有像 tab 那样的东西,并且自然不支持自动增量