data restored from mongodb dumb throwing bson error on _id field

45 Views Asked by At

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

1

There are 1 best solutions below

0
Joe On BEST ANSWER

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

  • concatenate a string of 24 zeros with the existing _id
  • take the last 24 characters of the result
  • convert that 24-character string to an ObjectId
  • output the resulting documents to a new collection named "newproducts"
db.products.aggregate([
  {$project: { 
     _id: {$toObjectId: 
         {$substr: [
             { $concat: ["000000000000000000000000","$_id"] },
             {$strLenCP:"$_id"}, 
             24
          ] } 
     }
 }},
{$out:"newproducts"}
])