Cosmos Mongo API - Unique and compound indexes do not support nested paths.

Prakash Singh Chauhan 1 Reputation point
2022-04-28T11:56:43.707+00:00

I have the following schema:

const addressSchema = new Schema<AddressSchema>(
  {
    street: String,
    postalCode: String,
    city: String
  },


const detailsSchema = new Schema<DetailsSchema>(
  name:string:
  address: AddressSchema
)

Trying to make a unique index based on name and address.postalCode as follows:

db.profiles.createIndex({ name: 1, "address.postalCode": 1 }, { unique: true });

but this throws the following error:

{
 "ok" : 0,
 "errmsg" : "Unique and compound indexes do not support nested paths.",
 "code" : 115,
 "codeName" : "CommandNotSupported"
}

Is this error expected or am I doing something wrong? What is the suggested solution for this?
I was thinking of combining two fields and create a new field that will act as the unique index. Just exploring more options.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,049 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 18,886 Reputation points Microsoft Employee
    2022-04-28T19:32:23.863+00:00

    Hi, @Prakash Singh Chauhan Welcome to the Microsoft Q&A forum, and thanks for reaching out.
    As we understand the ask here is you are facing an issue while creating a unique compound index please correct me if my understanding is wrong.
    Can you try with the use case mentioned in the document.
    db.coll.createIndex({"children.$**" : 1})

    Regards
    Geetha