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
Related
I have a document in production that have two fields with the same key, but i don't know the value of these fields, example:
{
"email":"email1#idk.com",
"email":"email2#idk.com"
}
When i'm trying to copy the collection to another database, it says that one document have a duplicate key. How can i search this document to mannually remove it? I've tried to run some aggregation querys and it doesn't work. Also, the collection have more than 1.000.000 of documents.
You can't have two fields with same key in a collection in MongoDB.
Your error on the duplicate key is not caused by this situation, but certainly by a unique index on the arrival collection.
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})
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
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 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).