mercredi 23 janvier 2019

Should I store unique string into _id if the string too long in MongoDB?

I am implementing a crawler to get data from Youtube and I need to store the id (ex: UCZ0zEZe8pWhmNpPdzwfxx4B) of each channel into my database (using MongoDB 4.0). I want to store these id given the following conditions:

  1. The id field must be indexed
  2. Not use too much memory for indexing
  3. No need to hash the id into other shorter string (if possible)

Currently, I have 2 solutions:

  • Solution 1: store channel_id and set the index like following
{
   "_id": ObjectId("522bb79455449d881b004d27"),
   "channel_id": "UCZ0zEZe8pWhmNpPdzwfxx4B",
}
db.channels.createIndex({'channel_id':'hashed'})

  • Solution 2: store channel_id into _id and using default index of _id
{
   "_id": "UCZ0zEZe8pWhmNpPdzwfxx4B",
}

The solution 1 satisfy condition (1), (2), (3) but field _id seems useless because channel_id will be used like primary_key for this collection

The solution 2 satisfy condition (1), (3) and need more memory for indexing

I think solution 1 is better in this case but is there any pros and cons? What is the best practices to save the long unique string (with indexing) in MongoDB ?

Aucun commentaire:

Enregistrer un commentaire