MongoDB : alter an existing index on a collection - mongodb

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/

Related

Fast query and deletion of documents of a large collection in MongoDB

I have a collection (let say CollOne) with several million documents. They have the common field "id"
{...,"id":1}
{...,"id":2}
I need to delete some documents in CollOne by id. Those ids stored in a document in another collection (CollTwo). This ids_to_delete document has the structure as follows
{"action_type":"toDelete","ids":[4,8,9,....]}
As CollOne is quite large, finding and deleting one document will take quite a long time. Is there any way to speed up the process?
Like you can't really avoid a deletion operation in the database if you want to delete anything. If you're having performance issue I would just recommend to make sure you have an index built on the id field otherwise Mongo will use a COLLSCAN to satisfy the query which means it will over iterate the entire colLOne collection which is I guess where you feel the pain.
Once you make sure an index is built there is no "more" efficient way than using deleteMany.
db.collOne.deleteMany({id: {$in: [4, 8, 9, .... ]})
In case you don't have an index and wonder how to build one, you should use createIndex like so:
(Prior to version 4.2 building an index lock the entire database, in large scale this could take up to several hours if not more, to avoid this use the background option)
db.collOne.createIndex({id: 1})
---- EDIT ----
In Mongo shell:
Mongo shell is javascript based, so you just have to to execute the same logic with js syntax, here's how I would do it:
let toDelete = db.collTwo.findOne({ ... })
db.collOne.deleteMany({id: {$in: toDelete.ids}})

Can mongodb create index when inserting data?

I've read the docs of mongodb and get to know that if I want to do text search I should use create index. But can I create index for each data during insert process? If I can, how should I do?
The index is created once for a collection, i.e. for text index do this
db.yourCollection.createIndex({yourText:"text"})
The index is updated automatically on every insert operation. You don't have to do this manually but have it in mind. This is what makes insert operations expensive. The more indexes you have the longer it takes to insert a document and update all of them.
If you want a link to the documentation, this topic is faced here.

How to disable mongodb index while update large collection

My requirement is to update or add an array field into large collection. I've index on filed "Roles". While update this collection it is taking arounf 3 miniutes .. Before creating index on "role" filed it was taking less than 40 sec to update/add fileds in the collection. We need the index to read the collection . But while update it makes trouble. Is it possible to disable index while update in mongodb.. Is there any funtions available with mongo? My mongodb version is 2.6.5
Please advice.
In Mongodb Indexes are updated synchronously with the insert/update. There is no way to pause the update of Indexes.
If your indexes are already created then you have two options
Drop the index and recreate the index, but it will have the following impacts
Queries executed at the time of the insert/update is happening will miss the index.
Rebuilding index is too expensive
Wait for the index to be updated
Queries will not use partially-built indexes: the index will only be usable once the index build is complete.
Source: http://docs.mongodb.org/manual/core/index-creation/
That means your index will block any query on the field/collection as long as the index is not complete. Therefore your have no chance but waiting for the index to be updated after adding new data.
Maybe try using another index.

Adding index on a compound index on 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})

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.