I have a database structure whose documents use a foreign key for referencing each other. Even though MongoDB don't do very well at JOIN query, we can get the documents by running separate queries based on the foreign Key.
I can use MongoDB's ObjectID (_id
) which is unique. But it's too long and also displaying this long id at client side for querying data looks little ugly. I thought of creating custom Auto-Incrementing in MongoDB which will solve the issue but MongoDB documentation says:
Generally in MongoDB, you would not use an auto-increment pattern for the _id field, or any field, because it does not scale for databases with large numbers of documents.
So what's the best solution for my below data structure
Collection: Products
{
Project_ID: 1001, <Auto Incrementing>
Product: "Example Product",
..<Few other fields>..
}
Collection: Payments
{
Project_ID: 1001, <Foreign key>
Pay_ID: 2001, <Auto Incrementing>
..<Few other fields>..
}
Collection: Others
{
Project_ID: 1001, <Foreign key>
Pay_ID: 2001, <Foreign key>
Other_ID: 3001, <Auto Incrementing>
..<Few other fields>..
}