"School with ID “601664bd3c7d8d38768c43b9” doesn’t exist. Perhaps it was deleted?" 使用 mongorestore 迁移时出错

"School with ID “601664bd3c7d8d38768c43b9” doesn’t exist. Perhaps it was deleted?" Error when migrated using mongorestore

我想将数据从 Mongodb 数据库 运行 Nodejs+mongoose 平台迁移到 Django+djongo 平台。我使用 mongodump 创建了数据备份并得到了一个用于迁移的 .bson 文件。

源数据库显示的数据如下

{
    "_id" : ObjectId("5c03c9f4a836690d6c540835"),
    "title" : "School A",
    "location" : "Location A",
    "postTime" : "1543752160981",
    "createdAt" : ISODate("2021-10-23T16:47:43Z"),
    "verified" : true,
    "__v" : 0,
    "image" : "http://21.441.45.80:3002/images/1543752161789-file.jpg",
    "thumbnail" : "http://21.441.45.80:3002/images/thumnail-1543752161789-file.jpg"
}

我创建了具有类似字段的目标数据库,避免了“__v”。我使用以下命令将源数据导入目标数据库。

mongorestore --db school_db --collection school_schoolmodel ../path/to/school.bson 

我收到消息

1169 document(s) restored successfully. 0 document(s) failed to restore. 然后我打开 django 管理面板,并单击其中一项。然后我得到以下错误?

School with ID “601664bd3c7d8d38768c43b9” doesn’t exist. Perhaps it was deleted?

这里有什么问题?

我把代码放在这里。如果需要任何其他详细信息,请发表评论。 我不允许更改源数据库的配置。

models.py

class NewsModel(models.Model):
    _id = models.CharField(max_length=20, primary_key=True, serialize=False,
                          verbose_name='Company Code')
    title = models.CharField(max_length=60, blank=True)
    location = models.TextField(blank=True)
    postTime = models.IntegerField(blank=True)
    createdAt = models.DateTimeField(blank=True)
    verified = models.BooleanField(default=False, blank=True)
    image = models.ImageField(upload_to=image_upload_to, blank=True)
    thumbnail = models.ImageField(upload_to=thumb_image_upload_to, blank=True)

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

    def __str__(self):
        return self._id

    class Meta:
        verbose_name = 'School'
        verbose_name_plural = 'Schools'

我想主键肯定在数据迁移过程中发生了变化。

更新 我使用 django admin app 创建了一条新记录,并将主键字段与迁移的记录进行了比较。在迁移的字段中,_id 值显示为字符串,而不是 ObjectId()。