Adding index on a compound index on Mongodb - mongodb

I created a compound index on my db using:
db.collection.ensureIndex({a:1,b:1})
Now I realized I need another level in the composition:
db.collection.ensureIndex({a:1,b:1,c:1})
Will Mongodb create a whole new index, or will it know to modify the existing one?

Calling ensureIndex with different fields will create a new index.
You can confirm this after running both commands by using getIndexes to see what indexes exist for your collection:
db.collection.getIndexes()
If you no longer want the original index, you can drop it using:
db.collection.dropIndex({a:1,b:1})

Related

How to drop selected multiple indexes for a collection in mongodb?

I want to drop multiple selected indexes by name using query in mongodb
I tried using dropIndex to drop multiple indexes.But this is deleting only first index i.e,index_1
db.getCollection('test').dropIndex( "index_1", "index_2" )
should delete index_1 and index_2
db.collection.dropIndexes()
Drops all indexes other than the required index on the _id field.
The db.collection.dropIndex() method takes the following parameter:
index (string or document)
Specifies the index to drop. You can specify the index either by the index name or by the index specification document.
So, it means you can not specify two indexes. To delete two indexes call db.collection.dropIndex() twice.
To obtain a list of documents that identify and describe the existing indexes on the collection use
db.collection.getIndexes()

Making two fields compound index in mongoDb and update the document

I'm using mongoDb in our project and got stuck at a point.
I'm using bulkOperation of mongoDb to save a list of Objects.
I need to make two fields (mac and gatewaytime ) as a compound index on a collection that make up an unique combination for documents in collection.
I wanted to update the whole document if any combination of mac and gatewaytime found with a the new document.
This scenario is similer to the _id index created with each document. Just the difference is that here I need to make a compound index of these two fields.
I found that we can make an compund index as Unique but this just reject any document if found to be duplicate. I need this duplicate document to be updated with the new document in my case.
If question is not undestandable, please let me know freely.

shardcollection with existing document mongodb

Let's say that I had a collection called "History" and "Employees" and there will a document on it.
When I create first collection with
sh.shardCollection("Databases.History",{"email":1})
it works fine, and I add some document on it.
But. When I create the second collections named "Employees" with
db.Employees.insert({"name":"Jhon Wick", "email":"jw#goodemail.com"})
and create a shard collections on it with
sh.shardCollection("Databases.Employees",{"email":1})
, it doesn't work.
MongoDB says that "please create an index that starts with the shard key before sharding."
so I tried add some index to Employees with db.Employees.ensureIndex({"email":"hashed"})
and it works. But when I tried shardCollection again, it still return that error.
What should I do?
Do MongoDB have a rules that if I would add shardKeys on a collection, it (collection) must not exist at first?
Your index strategy must be same as yout shard strategy. If you have created a hashed index, then you must specify hashed strategy while sharding.
sh.shardCollection("Databases.Employees",{"email":1})
shoule be replace with
sh.shardCollection("Databases.Employees",{"email":"hashed"})
https://docs.mongodb.com/v3.0/tutorial/shard-collection-with-a-hashed-shard-key/#shard-the-collection

is there any way to restore predefined schema to mongoDB?

I'm beginner with mongoDB. i want to know is there any way to load predefined schema to mongoDB? ( for example like cassandra that use .cql file for this purpose)
If there is, please intruduce some document about structure of that file and way for restoring.
If there is not, how i can create an index only one time when I create a collection. I think it is wrong if i create index every time I call insert method or run my program.
p.s: I have a multi-threaded program that every thread insert and update my mongo collection. I want to create index only one time.
Thanks.
To create an index on a collection you need to use ensureIndex command. You need to only call it once to create an index on a collection.
If you call ensureIndex repeatedly with the same arguments, only the first call will create an index, all subsequent calls will have no effect.
So if you know what indexes you're going to use for your database, you can create a script that will call that command.
An example insert_index.js file that creates 2 indexes for collA and collB collections:
db.collA.ensureIndex({ a : 1});
db.collB.ensureIndex({ b : -1});
You can call it from a shell like this:
mongo --quiet localhost/dbName insert_index.js
This will create those indexes on a database named dbName on your localhost. It's worth noticing that if your database and/or collections are not yet created, this will create both the database and the collections for which you're adding the indexes.
Edit
To clarify a little bit. MongoDB is schemaless so you can't restore it's schema.
You can only create indexes and collections (by using createCollection helper).
MongoDB is basically schemaless so there is no definition of a schema or namespaces to be restored.
In the case of indexes, these can be created at any time. There does not need to be a collection present or even the required fields for the index as this will all be sorted out as the collections are created and when documents are inserted that matches the defined fields.
Commands to create an index are generally the same with each implementation language, for example:
db.collection.ensureIndex({ a: 1, b: -1 })
Will define the index on the target collection in the target database that will reference field "a" and field "b", the latter in descending order. This will happen even if the collection or even the database does not exist as yet, or in fact will establish a blank namespace in that case.
Subsequent calls to the same index creation method do not actually re-create the index. Where the same index is specified to one that already exists it is effectively skipped as a "no-operation".
As such, you can simply feed all your required index creation statements at application startup and anything that is not already present will be created. Anything that already exists will be left alone.

MongoDB : alter an existing index on a collection

We are using MongoDB for our Application .
I see that an indexes are already present for the collections .
We had a new requirement , for which i see that the response is very slow from MongoDB for some of the opeartioons.
I want to add an extra field to the existing index , without dropping the existing index .
Please tell me if this is possible or not .
Will this have any impact on the Application ??
I am pretty sure you can't alter indexes once they have been created.
You could easily create a new index that would cover the new field.
Adding indexes is very easy from the shell you can type
db.yourcollection.ensureIndex( { field1: 1, field2: -1 } )
Mongo will look at your indexes and work out which one is best to use for your query. You can see this by adding explain onto the end of your query in the shell, this will tell you if it used an index and what index was used.
This will also be a good tool for working out what is slow about your query.
See the Mongo Documentation for further details http://docs.mongodb.org/manual/core/indexes/