0

I have a collection of documents where each holds a "list" property.

This list is an array of unique objects (I generated an ObjectID for each of them). We will call them listElements.

Now. I need to update 1 or more elements in the same query. I DO NOT NEED INDEX ON THEM. With arrays, I can't update each of them with its own specific values, it would only work as updating all matches with the same operation.

I thought of converting the "list" from an array, into an object who's keys are the ObjectID values so I could do:

{ $set: { "list.23rwfsdgfsda.time":123, "list.GJHhjh34j2h.order": "ascending" } }

This is just a placeholder example, I know its 12 characters for the key :P

My issue revolves around the keys: 12 characters is WAY TOO MUCH. I guesstimate around 300 possible listElements for a given list (possibly more) so a 3 character 16 base or even 10 base value would do fine.

Now, how would one generate an unique-per-"list" key for each new listElement while avoiding having to use a counter? It really doesn't matter to me what the key names are as long as they are short and unique-per-"list".

P.S: I don't need index because I will never find all documents in a collection based on the list and/or its content.

Discipol
  • 3,137
  • 4
  • 22
  • 41
  • You'll need something to "key" off of. Either a counter or something like a timestamp that won't be clobbered by rapid inserts. – WiredPrairie Aug 22 '13 at 11:23
  • Oh hi WiredPrairie :D have seen you around here. I would key off of server time but indeed, rapid inserts would be a bit of a problem. Thing is i only need them unique per document. And practically impossible for the client to do 2 inserts in the same millisecond. Is there any unique value I could get from the server other than current time? – Discipol Aug 22 '13 at 11:29
  • Trouble is that if there were, it would need to be a sufficiently large number that it wouldn't repeat, which seems to defeat your goal of it being small. An ObjectId for example would always be unique. – WiredPrairie Aug 22 '13 at 12:50
  • Yeah but its 12 characters :| for each key name. What about generating a 5 character number in base 64? (alphanumeric + $ + _)I think it goes to 1 billion possibilities, making 2 values for the same list being generated the same 0.00000...1% Ever toy with such things? – Discipol Aug 22 '13 at 12:55
  • If you look at all of the things that an `ObjectId` uses to stay unique (or a UUID for example), you'll see why this isn't a super easy problem to solve. How are you going to generate something short and unique (and efficiently)? Even if you remove some questionable aspects of an ObjectId (remove 5 bytes), you end up with a 12 character base64 encoded ID. – WiredPrairie Aug 22 '13 at 13:59
  • even that isn't 100% secure. Am I crazy or shortening the key name, over time added together, decreases my server query load significantly? – Discipol Aug 22 '13 at 14:36

0 Answers0