Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB indexing for different datatypes of same key

Suppose I have a three documents where "B" field is present. All the three have 3 different datatypes. How the Index BTREE is stored? And making "B" as an index key is efficient?

Example.

{_id:"1",a:1,b:"abc" }        //B is string here 
{_id:"1",a:1,b:[1,2,3] }      //B is array of numbers
{_id:"1",a:1,b:{x:1,y:"a"} }  //B is another nested document
like image 806
manikawnth Avatar asked Dec 11 '25 01:12

manikawnth


1 Answers

It doesn't work any different than if they are all the same type. It's not any more or less efficient than if they are all the same type. Just be aware of how sorting will work- different types will be compared by the BSON type ordering.

like image 192
wdberkeley Avatar answered Dec 13 '25 20:12

wdberkeley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!