質問

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!

役に立ちましたか?

解決

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")})

他のヒント

So, after digging deeper into the issue, it turns out lobster1234 was totally right. Moreover, this is a known bug with the Meteor framework.

https://github.com/meteor/meteor/issues/61

The workaround was to go back to the original dataset, which had some assetids from the old system, as so:

[
  {
    'assetid' : '123456789a',
    'assettype' : 'image',
    'title' : 'Cat',
  },
  {
    'assetid' : '123456789b',
    'assettype' : 'image',
    'title' : 'Dog',
  },
  {
    'assetid' : '123456789c',
    'assettype' : 'image',
    'title' : 'Bird',
  }
]

And to do a simple search-and-replace of 'assetid' into '_id', so the dataset was as follows:

[
  {
    '_id' : '123456789a',
    'assettype' : 'image',
    'title' : 'Cat',
  },
  {
    '_id' : '123456789b',
    'assettype' : 'image',
    'title' : 'Dog',
  },
  {
    '_id' : '123456789c',
    'assettype' : 'image',
    'title' : 'Bird',
  }
]

After running mongoimport, the _id fields were populated with the Strings from the dataset, rather than MongoDB generating unique ObjectIds.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top