djangomongodbdatabase-migrationmongorestoredjongo

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


I want to migrate the data from a Mongodb database running with a Nodejs+mongoose platform into a Django+djongo platform. I created the data backup using mongodump and got me a .bson file which I used for migration.

data shown in source database is as follows.

{
    "_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"
}

I created the destination database with similar fields avoiding "__v". I imported the source data into destination database using following command.

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

I got message that

1169 document(s) restored successfully. 0 document(s) failed to restore. Then I opened the django admin panel, and clicked one of the item. Then I got following error?

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

What is the issue here?

I'm putting code here. Please comment if any additional details required. I'm not allowed to change the configuration of source database.

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'

Solution

  • I think the primary keys must have changed during the data migration.

    Update I created a new record using django admin app and I compared the primary key field with the migrated record. In the migrated field, the _id value was shown as string and was not a ObjectId().