Duplicate _id in MongoDB Hack

Mongodb does not support duplicate _id as it being index which is internally created by mongodb if not provided but in the next sections I will explain a simple work around for that. Mongodb creates index on _id by default meaning you don’t need to create index on _id. Index on _id is fastest search of all indexes you can create in mongodb. In my point of view developer should always make use of _id as one of its primary key to search. When your collections will contain millions of documents then you will be fond of _id by its speed of search. Mongodb let you create indexes on any field of the document but keep in mind that indexes cost memory and the important fact is whether you want or not mongodb will create index on _id thereby utilizing some of your memory for storing those indexes. _id index is always unique by default and you can not impose duplicate by recreating index on it.
db.collection.createIndex({ _id:1 }, { unique: false }).

It will generate error telling that InvalidIndexSpecificationOption. There is no way you can have two documents in a collection having same _id but there is a hack for that I am going to explain in the below sections.
Scenario for duplicate _id.
Consider the case when you have a collection named transactions which stores all the tractions by individual users. One user may have multiple transactions. And your requirement is to search the collection using unique userid for a particular user’s transactions. Here you can create index on the userid if you already know that your collection is going to store millions of transactions. But creating index on userid will costs you memory to store along with the indexes of _id which mongodb creates by default. The challenge is how we can use _id here keeping in mind that _id can not except duplicate and in our case if we use _id to store userid then it will be duplicate and mongodb will reject to store. In next section I will explain the hack to utilize _id to store userid.
Hack for Utilizing duplicate _id.
All our concerns are to utilize the power of _id which it provides for search. So that we will not be bound to create extra indexes on the collections. So here we go!. We can store userid in _id with some tricks, look at the below code snippet.
_id = userid+randomString(); // the randomString must be some kind of uuid.

The above assignment will always create a unique _id even if we use same userid for multiple transactions as the randomString will always be different as it is a kind of uuid. This way we can use our userid in _id to store and do not need to create extra index as mongodb is bound to create index on _id. Now the problem is how we can make a search on the collection as we have created custom _id which is now a totally different string after concatenating userid with randomString. Here we already know that the _id contains two parts first part is userid and the second part is randomString. This information will solve our problem of solving search.
Searching the custom _id.
As we know that _id contains userid and randomString. You have to make sure that userid must be the first part of _id. The search will be done by monodb $regex. The mongodb query will look like
db.collection.find({ _id:{ $regex: /^userid/ } }); // see the caret ^ it's very important.
Mongodb $regex search exploits the index only when the regular expression in our search query is a prefix expression which means the regex string in our query must be the first part of indexed field value as in our case _idcontains userid+randomString where userid is the first part of _id and the regex string in our query have a ^ meaning that _id starting with userid only. This way mongodb creates a range of all those transactions where the _id values will start with the userid in our case utilizing the index scan. If we don’t tell the mongodb that the queried field should start with userid then the mongodb will scan the whole collection indexes and the regex search will become a nightmare.