Re: Using complex _id fields with the aggregation framework (dictionaries as _id fields)

32 views
Skip to first unread message

Thomas Rueckstiess

unread,
Nov 19, 2012, 2:03:16 AM11/19/12
to mongod...@googlegroups.com
Hi Lothar, 

Are you just after a uniqueness guarantee of several of the fields in combination (key1 and key2 in your example)? MongoDB supports unique indexes, both on single and compound fields. The _id field automatically has a unique index: http://docs.mongodb.org/manual/core/indexes/#index-type-id

But additional unique indexes on compound fields can be enforced as well: 

Perhaps this solves your problem already and you won't have to deal with documents as _id.

Thomas



On Saturday, November 3, 2012 2:42:59 AM UTC+11, Lothar Braun wrote:
Hi,

I'm trying to use dictionaries as _id fields for some of my collections. The _id field is one example is defined to be

_id : { key1: <somevalue>, key2: <someothervalue> }

When this key is matched in an aggregation pipeline, the output depends on the order of the searched fields. The pipeline  

[{$match : { "_id" : { "key1" : "<somevalue>", "key2" : "<someothervalue" } }}]

can produce a match while the pipeline 

[{$match : { "_id" : { "key2" : "<someothervalue>", "key1" : "<somevalue" } }}]

does not produce a match (note that the order of the fields in the dictionary have been swapped). The proper order of the keys must not match the order in which they are defined when inserting or updating the document. The order seems to be defined by mongo and in my experiments always resulted in alphabetical order, e.g. if I used the names"bsdf" and "asdf" as key1 and key2 "asdf" always was the key that needed to be specified first in the $match query.

Does someone know if this is the desired behaviour? (I'm using mongo 2.2.1)

Using such _id fields it wasn't possible for me to define matching that requests more than one document. E.g. it was not possible to request documents which have "_id : { "key1": <somevalue>"). Is this the desired behaviour or did I just not understand the documentation?

The reason why I'm trying to do is that I want to use "key1" and "key2" as what would be primary keys in the SQL world. If I cannot do any of the operations above, what would be the proper way to do this in mongo (or isn't there any?)

Best regards,
  Lothar 

Reply all
Reply to author
Forward
0 new messages