Search code examples
mongodbmeteormongoimport

collection _id doesn't have dashes after mongoimport?


So, I'm having a problem with an SQL to Mongo data migration. We took our SQL database and flattened it into a single array of data, with a basic array structure, like so:

[
  {
    'assettype' : 'image',
    'title' : 'Cat',
  },
  {
    'assettype' : 'image',
    'title' : 'Dog',
  },
  {
    'assettype' : 'image',
    'title' : 'Bird',
  }
]

Very simple, very straight-forward. Once we had it in array form, we imported it into our mongo instance, as follows.

mongoimport -d staging -c assets < library_assets.json --jsonArray

Again, very simple. Very straight-forward. On first glance, it appeared that everything was working as expected. But, upon closer inspection, it's turned out that the MongoIds of all the records in the assets collections don't have dashes! That is, all the mongo ids are in the following format:

51073797074f0d6db8e3149a

Instead of the expected format:

c6689c53-a05c-4e94-b503-ac61558cc0c6

Everything works find, except for queries that rely on the MongoId. So, for instance, this will work:

db.assets.find();

But this will not:

db.assets.findOne({"_id": 51073797074f0d6db8e3149a });

I've been pouring through documentation and goolging, but am not finding anything. If anybody could point me in the right direction and help import this file so it has the correct MongoId formats, I'd really appreciate it!


Solution

  • I am not sure why you are expecting dashes in the _id field - if they're auto generated by Mongo (i.e. you did not have a _id field in your import), they'd look something like 4f5fbb91a717b0f8d080e9d7. So, what you're seeing is perfectly normal result of a successful import with no user-defined _id field.

    That said, the _id, when auto-generated, is an instance of ObjectId and not a String. This query will work:

    db.assets.findOne({"_id": new ObjectId("51073797074f0d6db8e3149a")})