I am planning to set the _Id field as a String which is a summation of two fields from the document
This to create a unique identifier as well as to make use of the unique constraint of _id field
Could you let me know if this is the correct approach. Would it have any impacts while Sharding etc
You cannot have an _id field that is dynamically computed from 2 fields. However you may have an object that contains your 2 fields as the _id. For example:
Both object equality is tricky. Object { a:1 , b:2 } is not equal to object { b:2 , a:1 }. As a consequence, you may have an document, in the previous example, where _id is “first”:“Steeve” and “last”:“Juneau”. From mongosh:
db.test.insertOne( { _id : { "first" : "Steeve" , "last" : "Juneau" }})
{ acknowledged: 1,
insertedId: { first: 'Steeve', last: 'Juneau' } }
db.test.find()
{ _id: { last: 'Juneau', first: 'Steeve' } }
{ _id: { first: 'Steeve', last: 'Juneau' } }
// So we now have 2 object with different _id but logically refers to the same person.
// This is problematic with some queries as seen below:
> db.test.find( { "_id.last" : "Juneau" , "_id.first" : "Steeve"})
{ _id: { last: 'Juneau', first: 'Steeve' } }
{ _id: { first: 'Steeve', last: 'Juneau' } }
// But we have
> db.test.find( { "_id" : { "last" : "Juneau" , "first" : "Steeve"} } )
{ _id: { last: 'Juneau', first: 'Steeve' } }
This might not be an issue. Because if your db creation is done with a well define API you should expect to always insert in the same order. You just need to be aware.
Note that array elements order is also important. Array [1,2] is not equal to array [2,1].