databasemongodbmongorestoredata-dump

data restored from mongodb dumb throwing bson error on _id field


Downloaded MongoDB dump from https://world.openfoodfacts.org/data

I am using the below command to take the data to my DB

mongo-restore -h localhost:27017 -d db-name /off/dump/products/

products folder has these two files: products.bson and products.metadata.json

After restoring the value of _id is not coming as a type of Mongodb Objectid. it's a string eg: _id: "0000000000"

I want the _id to be mongodb ObjectId


Solution

  • You could use aggregation to convert the strings to ObjectId and write the results to a new collection.

    Note that this will only work if all of the existing _id values contain only 0-9 and a-f.

    The example you show is not a valid ObjectId, this pipeline will

    db.products.aggregate([
      {$project: { 
         _id: {$toObjectId: 
             {$substr: [
                 { $concat: ["000000000000000000000000","$_id"] },
                 {$strLenCP:"$_id"}, 
                 24
              ] } 
         }
     }},
    {$out:"newproducts"}
    ])