Paul
Paul

Reputation: 665

MongoDb Index/Find on List of Lists

I'm using the C# mongodb driver to serialize a Dictionary into an embedded document. The embedded document looks something like:

"Lookup" : [[1234, {
    "Name" : "bob",
    "Age" : 25,
  }], [4567, {
    "Name" : "fred",
    "Age" : 31,
  }]]

Is it possible to create an index just on the "key" (i.e. 1234, 4567 etc) of the list? I've created an index on Lookup but I'm not sure what it's created an index on.

I'm guessing it's indexed the whole document because the query:

find ( {"Lookup" : { "$in" : [1234] } } )

Doesn't match anything.

Thanks,

Upvotes: 2

Views: 1389

Answers (1)

mstearn
mstearn

Reputation: 4276

You should change your structure to look something like this:

"Lookup" : [
    {key:1234,  value:{
      "Name" : "bob",
      "Age" : 25,
    }},
    {key: 4567, value: {
      "Name" : "fred",
      "Age" : 31,
    }}
]

Then you could index {"Lookup.key": 1} and it will do what you want.

Upvotes: 1

Related Questions