I have sharded collection with shard key as hashed _id (docs ).
Now I want to add index on another field (called alias) which doesn't need to be unique but it should be sparse if possible. This new index would serve only for speeding my query.
Should I do some kind of compound index for this or there is another procedure if possible in the first place?
In app I make only 2 types of queries :
db.myCollection.findOne({alias : 'some-alias'})
db.myCollection.findOne({_id : 'some-id'})
The second one works as supposed to (because my shard key is on hashed _id field) but first query is problem.
Thanks,
Ivan
Related
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
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.
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.
I have a collection with a compound key:
db.stuff.insert( {"_id":{"aid":"123","brand":"acme"},"name":"Greg"} )
The compound key ensures uniqueness in a multi-tenant environment. For this application the order being constant for BSON is fine.
My question is this: Can I find all the 'stuff' with brand = "acme" (i.e. use one part of the compound key in a query)? If it's possible will it utilize the index?
Sure, use a simple find():
db.stuff.find({"_id.brand" : "acme"});
I am building a webapp using Codeigniter (PHP) and MongoDB.
I am creating indexes and have one question.
If I am querying on three fields (_id, status, type) and want to
create an index do I need to include _id when ensuring the index like this:
db.comments.ensureIndex({_id: 1, status : 1, type : 1});
or will this due?
db.comments.ensureIndex({status : 1, type : 1});
You would need to explicitly include _id in your ensureIndex call if you wanted to include it in your compound index. But because filtering by _id already provides selectivity of a single document that's very rarely the right thing to do. I think it would only make sense if your documents are very large and you're trying to use covered indexes.
MongoDB will currently only use one index per query with the exception of $or queries. If your common query will always be searching on those three fields (_id, status, type) then a compound index would be helpful.
From within the DB shell you can use the explain() command on your query to get information on the indexes used.
You don't need to implicitly create index on the _id field, it's done automatically. See the mongo documentation:
The _id Index
For all collections except capped collections, an index is automatically created for the _id field. This index is special and cannot be deleted. The _id index enforces uniqueness for its keys (except for some situations with sharding).