How to drop selected multiple indexes for a collection in mongodb? - 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()

Related

In Mongodb how can I view an index's values

I not asking how to view the indexes on a collection but how can I look inside the index and see its values?
I have a field that should be unique so I created a unique index and now I want to cross verify that all the documents are present in the index.
Normally you cannot look inside the index. It's just linked list. But... You can do count from index. db.data.find({},{"_id":1}).hint({"_id":1}).itcount()
In that example I project only field _id, with hint() I ordered system use unique index of "_id" and with itcount() I ordered NOT to use metadata information of count, but go thru that find cursor and do count of every item.

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})

Does Mongodb automatically updates indexed items? [duplicate]

Lets say you have a collection with a field called "primary_key",
{"primary_key":"1234", "name":"jimmy", "lastname":"page"}
and I have an index on "primary_key".
This collection has millions of rows, I want to see how expensive is to change primary_key for one of the records. Does it trigger a reindex of the entire table? or does it just reindex the changed record? in either case is that expensive to do?
Updating an indexed field in mongodb causes an update of the index (or indices if you have more than one) that use it. It does not "reindex". Shouldn't be all that expensive - effectively you will delete the old entry and insert a new one.
This document has a fair amount of detail on mongodb indexes:
http://docs.mongodb.org/master/MongoDB-indexes-guide.pdf
BTW, keep in mind that there is one special field, _id, that mongodb uses as it's primary key
_id
A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document’s
primary key. If you create a new document without an _id field,
MongoDB automatically creates the field and assigns a unique BSON
ObjectId.
You cannot update the _id field.

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})

How does mongodb index lists

For example: If I had a db collection called Stores, and each store document has a list of the items they sell, and stores generally share items, then how would mongodb build an index on that?
Would it build a btree index on all possible items and then on each leaf of that tree (each item) will reference the documents which contain it?
Background:
I'm trying to perform queries like this using an index:
db.store.find({merchandise:{$exists:true}}) // where 'merchandise' is a list
db.store.find()[merchandise].count()
would an index on 'merchandise' help me?
If not, is my only option creating a separate meta field on 'merchandise' size, and index that?
Schema:
{ _id: 123456,
name: Macys
merchandise: [ 248651234564, 54862101248, 12450184, 1256001456 ]
}
From your document sample if you build your index on merchandise it will be multikey index and that index will be on every item on the array. See Multikey Indexes section in here.
If merchandise is an array of subdocuments, indexing over merchandise will put the index on all field of subdocument in the array. With index you can make queries like
db.store.find("merchandise":248651234564) and it will retrieve all document having merchandise 248651234564
For getting count of merchandise, you can get only get the size of merchandise field of one document like db.store.find()[index].merchandise.length. So creating a seperate field on merchandise size and indexing is a feasible option, if you want to run queries based on merchandise size.
Hope this helps
If you index a field that contains an array, MongoDB indexes each value in the array separately, in a multikey index. When you have 4 documents inside an array, each will act as a key in the index and point to the mentioned document(s).
You can use multikey indexes to index fields within objects embedded in arrays. That means, in your array, you can index a specific field in each document. For example: stuffs.thing : 1.
Read more about Multikey Indexes
Whether you need these indexes would depend on:
How many queries rely on that specific field?
How many updates, inserts hit that specific field (array)?
How many items will that array contain?
...
Remember that indexes slow writes as they need to be updated as well. I'd consider an explain on my queries to measure performance.