0
db.test.insert({a:1, b:[1])
db.test.insert({a:1, b: 1})
db.test.insert({a:[1], b: 1})
db.test.insert({a:[1], b: [1]})

what should be a right way to index the fields so that i can query over a and b?

1 Answer 1

1
{ _id: 1, a: [1, 2], b: 1, category: "A array" }
{ _id: 2, a: 1, b: [1, 2], category: "B array" }

A compound multikey index { a: 1, b: 1 } is permissible since for each document, only one field indexed by the compound multikey index is an array; i.e. no document contains array values for both a and b fields.

However, after creating the compound multikey index, if you attempt to insert a document where both a and b fields are arrays, MongoDB will fail the insert.

---- Documentation

But one of your doc's a & b both contain array.

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

2 Comments

So for this kind of document scopes , any index is not possible ?
Documentation says so.

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.