2

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,

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.