Can a query still use an index to sort a field if the index was not the one chosen in the winning plan? - mongodb

I have a compound index and an index on a single field A. If in a find query, the compound index was chosen as the winning plan, and the results are sorted by the field A, will field A's index be used to sort it?

Unfortunately, no.
MongoDB cannot use different indexes for sorting and document selection.
See https://www.mongodb.com/docs/manual/tutorial/sort-results-with-indexes/#use-indexes-to-sort-query-results
Note that it can use a compound index for sorting, i.e. if the compound index were on {a:1, otherfield:1}, that index can be used for selection by multiple fields, and sorting by a.

Related

MongoDB query with only one field of a compound index

Let's say I have a compound index that uses the following two fields in order: GroupId, NameId. Then at some point I want to query the collection, but I only have access to the NameId, then how does MongoDB do this search, if the first field(s) of a compound index aren't used for the query? Is linear search used for each Group, but then it utilizes the NameId since the NameIds are sorted within each group? Or does it ignore the NameId field too and only use linear search?
In short, can field A of the compound index "sabotage" and force linear search for fields B and/or C if not A is used? Or is binary search still used for the B and C fields?

Get Number of Documents in MongoDB Index

As per the title, I would like to know if there is a way to get the number of documents in a MongoDB index.
To be clear, I am not looking for either of the following:
How to get the number of documents in a collection -- .count().
How to get the size of an index -- .stats().
An index references all of the documents in its collection unless the index is a sparse index or a partial index. From the docs:
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field. The index is “sparse” because it does not include all documents of a collection. By contrast, non-sparse indexes contain all documents in a collection, storing null values for those documents that do not contain the indexed field.
Partial indexes only index the documents in a collection that meet a specified filter expression
So ...
The answer for non sparse and non partial indexes is db.collection.count()
The answer for sparse and partial indexes could be inferred by running a query with no criteria, hinting on that index and then counting the results. For example:
db.collection.find().hint('index_name_here').count()

Create geospatial and type index together in MongoDB?

I want to use two indexes from different index types (2dsphere and text) by using this command :
db.mycoll.createIndex({"#geolocationable":"2dsphere",name:"text"})
but I get the following Error:
"errmsg" : "bad index key pattern { #geolocationable: \"2dsphere\",
name: \"text\" }: Can't use more than one index plugin for a single
index."
I read MongoDB Text and 2D compound index
but I'm not sure that why I can't create 2dsphere and text index in one collection.
I don't mean that I want to use of both indexes in one query while I want to create this indexes in order to use from them in separate queries individually
Edit: Modified to answer the updated question.
If both the fields are to be used in queries separately, then you can create 2 different indexes instead of the compound index.
The geospatial index:
db.mycoll.createIndex({"#geolocationable":"2dsphere"})
The text index:
db.mycoll.createIndex({name:"text"})
Also, from docs note that
A collection can have at most one text index.
While creating a compound index, text index can not be grouped with a multi or geospatial index. It is a compound index restriction.
From the docs:
A compound text index cannot include any other special index types,
such as multi-key or geospatial index fields.
However, if you are not going to perform case insensitive searches on name field, you can create the compound index with a normal index instead of a text index.
db.mycoll.createIndex({"#geolocationable":"2dsphere",name:1})

What does the digit "1" mean when creating indexes in mongodb

I am new to mongodb and want to make indexes for a specific collection. I have seen people use a digit "1" in front of the field name when they want to create an index. for example:
db.users.ensureIndex({user_name: 1})
now I want to know what does this digit mean and is it necessary to use it?
It's the type of index. MongoDB supports different kinds of indexes. However, only the first two indexes can be combined to a compound index.
1: Ascending binary-tree index.
-1: Descending binary-tree index. Very similar to the default index but the difference can matter for the behavior of compound indexes.
"hashed": A hashtable index. Very fast for lookup by exact value, especially in very large collections. But not usable for inexact queries ($gt, $regex or similar).
"text": A text index designed for searching for words in strings with natural language.
"2d": A geospatial index on a flat plane
"2dsphere": A geospatial index on a sphere
For more information, see the documentation of index types.
It defines the index type on that specefic field. For example the value of 1 creates an index with ascending order, while the value -1 create the index with descending order.
For more information, see the Manual

mongodb index query

If I have a collection which I query based on fields _id and CreationTimestamp.O as below:
var _query = Query.And(
Query.EQ("_id", "TheId"),
Query.GT("CreationTimestamp.0", DateTimeOffset.UtcNow.AddMinutes(-15).UtcTicks));
Should I create a compound index on both these fields?
I know _id by default has an index.
I am looking for guidance on best practice i.e. Create a compound index for both fields or just create an index for CreationTimestamp.O as their is already an index on _id
There's not much benefit in creating compound index here. Since the selectivity of autocreated index on _id field is 1 (it's unique index) there's no reason in having any compound index prefixed with it. The only possible benefit may be the fact MongoDB can use covered index for this query and possibly speedup the things (you need to benchmark on your data to prove this).