I have a simple collection alert with _id and observables array
e.g.
{
_id: '60d496b2d08d5f0630dea368',
observables: ['10d496b2d08d5f0630dea368', '20d496b2d08d5f0630dea368'],
}
I created query to create new collection without array in format like:
{
_id: <default id>,
alertId: '60d496b2d08d5f0630dea368',
observableId: '10d496b2d08d5f0630dea368',
{
_id: <default id>,
alertId: '60d496b2d08d5f0630dea368',
observableId: '20d496b2d08d5f0630dea368',
}
Also, to prevent from duplication, unique index was created
collection.createIndex({ observableId: 1, alertId: 1 }, { unique: true });
And here is my query:
db.collection.aggregate([
{
"$unwind": "$observables"
},
{
"$project": {
_id: 0,
observableId: "$observables",
alertId: "$_id"
}
},
{
"$merge": {
into: {
db: "some-db",
coll: "some-coll"
},
on: [
"observableId",
"alertId"
]
}
}
])
And as result got an error *MongoError: Cannot find index to verify that join fields will be unique
But the index was created, so any idea how to fix this error?
Link to mongoplayground