What's the purpose of specifying IndexDirection for indexes in MongoDB? - mongodb

I am using Morphia to communicate with MongoDb. When annotating fields with #Indexed, it's allowed to specify the IndexDirection of DESC, ASC, BOTH, GEO2D, what's the purpose of those separately?
If I specify BOTH, does it mean that there would have two indexes created?
And also, if I want the filtered entities to be order in descending order, does it mean that I'd better to have the index to have IndexDirection of DESC?

DESC = Descending, ASC = Ascending, and GEO2D = 2D/Geography
BOTH is not a valid option unfortunately. That was a mistake of the enum definition and no longer exists.
They indicate how the values are stored in the index. For a single index the direction is not important.
For compound indexes you can specify the orders for each field and it will make a difference. See the docs on the mongodb site for specifics.

Related

Does mongodb create index for items that do not match fitler in partial index with unique constraint?

The documentation here says that it allows insertion of the item, which is what I want. But I also want to index the field but without the unique constraint if the filter doesn't match i.e, if the partial filter matches, I want the filed to be unique, if it doesn't match, I don't want it to be unique, but still needs to be indexed.
I'm getting similar query time for queries that match the filter and which doesn't. 62809ns for that matching the filter, 65098ns for query that doesn't match the filter.
I ran the test on collection with 10Mill docs. I ran this multiple times and doing db.collection.getPlanCache().clear() before each query and am getting similar result each time. So, is it safe to assume that indexes are being created for both?

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?

DB Compound indexing best practices Mongo DB

How costly is it to index some fields in MongoDB,
I have a table where i want uniqueness combining two fields, Every where i search they suggested compound index with unique set to true. But what i was doing is " Appending both field1_field2 and making it a key, so that field2 will be always unique for field1.(and add Application logic) As i thought indexing is costly.
And also as MongoDB documentation advices us not to use Custom Object ID like auto incrementing number, I end up giving big numbers to Models like Classes, Students etc, (where i could have used easily used 1,2,3 in sql lite), I didn't think to add a new field for numbering and index that field for querying.
What are the best practices advice for production
The advantage of using compound indexes vs your own indexed field system is that compound indexes allows sorting quicker than regular indexed fields. It also lowers the size of every documents.
In your case, if you want to get the documents sorted with values in field1 ascending and in field2 descending, it is better to use a compound index. If you only want to get the documents that have some specific value contained in field1_field2, it does not really matter if you use compound indexes or a regular indexed field.
However, if you already have field1 and field2 in seperate fields in the documents, and you also have a field containing field1_field2, it could be better to use a compound index on field1 and field2, and simply delete the field containing field1_field2. This could lower the size of every document and ultimately reduce the size of your database.
Regarding the cost of the indexing, you almost have to index field1_field2 if you want to go down that route anyways. Queries based on unindexed fields in MongoDB are really slow. And it does not take much more time adding a document to a database when the document has an indexed field (we're talking 1 millisecond or so). Note that adding an index on many existing documents can take a few minutes. This is why you usually plan the indexing strategy before adding any documents.
TL;DR:
If you have limited disk space or need to sort the results, go with a compound index and delete field1_field2. Otherwise, use field1_field2, but it has to be indexed!

What's the behavior of db.find.sort()?

For example this is our document schema, which has an descending index on created_at field.
var titles = {
title: String,
created_at:{
type: Date,
index: -1
}
}
Here are 2 questions:
1st:
If we call db.titles.find() or db.titles.findOne() how result order will be?
I mean it will returns objects in desc or asc order?
2nd:
How about here?
db.titles.find().sort({created_at: -1})
How does MongoDB behave in the code above? Sort the result on the fly or it just use index order which we defined in the schema?
1st: If we call db.titles.find() or db.titles.findOne() how result
order will be? I mean it will returns objects in desc or asc order?
It will return documents in natural order - in order in which documents are stored on disk. If you want to sort documents by created_at you should explicitly specify it:
db.titles.find().sort({created_at: -1})
How does MongoDB behave in the code above? Sort the result on the fly
or it just use index order which we defined in the schema?
Since you already have an index on created_at field, Mongo will use it. But if you'll add some query
db.titles.find({title: /Foo/}).sort({created_at: -1})
then Mongo will no longer be able to use your index to sort query results and will be forced to sort it on the fly.
You can find more aboun MongoDB indexes in this blog post.

How do i create an index in mongodb on a WHERE and ORDER query?

In mongo, When creating an index I am trying to figure out whether the following query would have an index on a) category_ids and status, OR b) category_ids, status and name???
Source.where(category_ids: [1,2,3], status: Status::ACTIVE).order_by(:name) # ((Ruby/Mongoid code))
Essentially, I am trying to figure out whether indexes should include the ORDER_BY columns? or only the WHERE clauses? Where could I read some more about this?
Yes, an index on thius particular query would be beneficial to the speed of the query. However there is one caveat here, the order of the index fields.
I have noticed you are using an $in there on category_ids. This link is particularly useful in understanding a little complexity which exists from using an $in with an index on the sort (or a sort in general in fact): http://blog.mongolab.com/2012/06/cardinal-ins/
Towards the end it gives you an indea of an optimal index order for your type of query:
The order of fields in an index should be:
First, fields on which you will query for exact values.
Second, fields on which you will sort.
Finally, fields on which you will query for a range of values.
For reference a couple of other helpful links are as follows:
http://docs.mongodb.org/manual/applications/indexes/
http://docs.mongodb.org/manual/faq/indexes/#how-do-you-determine-what-fields-to-index
http://jasonwilder.com/blog/2012/02/08/optimizing-mongodb-indexes/
why does direction of index matter in MongoDB?
And, http://www.slideshare.net/kbanker/mongo-indexoptimizationprimer
These will help you get started on optimising your indexes and making them work for your queries.