1

Assume the following data structure in MongoDB:

{
    "_id" : "Bob Blocker",
    "ratings" : {
        "771206753" : 1
    },
    "prevalence" : 1
}

How could one index the keys of the internal structure of ratings using ensureIndex()? I would want to build an index that associated the document _id with any and all of the keys (in this example, "771206753") in the ratings object.

Would keying the index with ratings suffice? Wouldn't that also record the value portion of the ratings object? Should I shift my schema if lookup is intended by that particular key?

1 Answer 1

2

You can't index dynamic keys and indexing ratings would index the whole object as a blob so you don't want to do that either.

It may work better to rework your schema to:

{
    "_id" : "Bob Blocker",
    "ratings" : [
        {id: "771206753", value: 1}
    ],
    "prevalence" : 1
}

and then index 'ratings.id'.

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.