0

We see in one of our services that certain queries always result in a COLLSCAN, instead of the appropriate index being used. The data has the form

{_id: DocumentID, "field": {"child": "Cross-App-ID"}}

with an Index {"field.child":1}.

Running the query for "field.child", i.e. db.collection.find({"field.child":"CA-ID"}) works as expected with the index being used. However, for some reason the app accessing the database creates the queries as db.collection.find({"field":{"child":"CA-ID"}}), which results in a COLLSCAN instead of an IXSCAN.

2 questions for me from this:

  • Shouldn't these queries be equivalent and thus treated the same?
  • How can I create an index for the second use-case? All examples I found create the index like we already did.

1 Answer 1

0

2 questions for me from this:

  • Shouldn't these queries be equivalent and thus treated the same?
  • How can I create an index for the second use-case?

The queries return same results, but from indexing point they are considered different.

Index { "field.child": 1 }, works with queries of the shape db.test.find({"field.child": "ABCD" }).

And, the index { field: 1 } works with the query shape db.test.find({field: { child: "ABCD" }}).

You can create both the indexes, and they will be used with their respective queries.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.