1.

db.sample.find( { b : 1 } ).sort( { c : 1, a : 1 } )   db.sample.find( { c : 1 } ).sort( { a : 1, b : 1 } )   db.sample.find( { a : 1 } ).sort( { b : 1, c : 1 } )

Answer»

When we create any collection in MongoDB, a UNIQUE index on the _id field is created automatically. This unique index prevents applications from inserting multiple same DOCUMENTS with the same value for the _id field. This is forced by the system and we cannot drop this index on the _id field. Moreover, in replica sets the unique _id values are used in the oplog to reference documents to update.

In a sharded CLUSTER if we do not have unique _id values across the sharded collection chunk migrations MAY fail as when documents migrate to another shard, any identical values will not be inserted to receiver shard. In such cases, we should code application such that it ensures uniqueness on _id for given collection across all shards in a sharded cluster.

If we use _id as the shard key, this uniqueness of values will automatically be forced by the system. In such case chunk ranges will be assigned to SINGLE shard and then the shard will force uniqueness on the values in that range.



Discussion

No Comment Found