Hi All,
We have a document as below. Need to have only unique combination of productId and serviceId in “arrayfileds” array.
We have added unique index as below, but still duplicates with serviceId , productId combination are being allowed.
Could you help is restricting the duplicates in array.
Index:
db.collection.createIndex( { _id : 1, “subscriber.msisdn”: 1, “arrayfileds.serviceId”: 1, “arrayfileds.productId”: 1 }, { unique: true } )
db.collection.find({"_id" : "669486112345"}).pretty()
{
"_id" : "669486112345",
"subscriber" : {
"msisdn" : "669486112345",
"state" : 1,
"createdOn" : "2020-04-23 22:13:35.228",
"updatedOn" : "2020-04-23 22:13:35.228",
"lbChargeCode" : "",
"userClass" : "1",
"imei" : "",
"tg" : {
},
"arrayfileds" : [
{
"subsId" : "5217331305876549695359482",
"state" : 1,
"prvsState" : 0,
"createdOn" : "2020-04-23 22:16:09.584",
"updatedOn" : "2020-04-23 22:16:09.584",
"productId" : 4000,
"opid" : 0,
"reqChrgCode" : "49880127010",
"cCode" : "49880127010",
"lbPrice" : 0,
"serviceId" : 3000,
},
{
"subsId" : "5217331305876549695359482",
"state" : 1,
"prvsState" : 0,
"createdOn" : "2020-04-23 22:16:09.584",
"updatedOn" : "2020-04-23 22:16:09.583",
"productId" : 4000,
"opid" : 0,
"reqChrgCode" : "49880127010",
"cCode" : "49880127010",
"lbPrice" : 0,
"serviceId" : 3000,
},
{
"subsId" : "5217331305876549695359482",
"state" : 1,
"prvsState" : 0,
"createdOn" : "2020-04-23 22:16:09.584",
"updatedOn" : "2020-04-23 22:16:09.583",
"productId" : 4000,
"lbPrice" : 0,
"serviceId" : 3000,
}
]
}