Hi,
Trying to remove multiple items from multiple documents using $pull. here is the structure of the document in assets collection:
{
'asset_name': 'asset1',
'asset_unique_name': 'asset1_unique',
'connections': [
{
'connection_name': 'conn1',
'connection_unique_name': 'conn1_unique',
'id': 'conn_id_1',
'data': {
'value': 7,
'id': 'data_id_1'
}
},
{
'connection_name': 'conn2',
'connection_unique_name': 'conn2_unique',
'id': 'conn_id_2',
'data': {
'value': 1,
'id': 'data_id_2'
}
},
{
'connection_name': 'conn3',
'connection_unique_name': 'conn2_unique',
'id': 'conn_id_3',
'data': {
'value': 12,
'id': 'data_id_3'
}
}
]
}
I want to remove elements from the connections array by id from all the documents in the collection. here is what I’m trying:
db.getCollection('assets').update(
{'connections':
{'$elemMatch':
{'id':
{'$in': ['conn_id_2', 'conn_id_3']}
}
}
},
{'$pull':
{'connections':
{'id':
{'$in':['conn_id_2', 'conn_id_3']}
}
}
}
)
I’m getting E11000 duplicate key error collection
index: connections.connection_unique_name_1_connections.data.id_1 dup key: { connections.connection_unique_name: null, connections.data.id: null }
I assumed I have some keys with null values, I searched for connection_unique_name = null, I get 0 results.
I tried using updateMany, setting upsert=false, multi=true. didn’t work, does not return an error, but returned 0 results.
I tried also putting empty find object {} - still 0 results, nothing was removed.
db.getCollection('assets').update(
{},
{'$pull':
{'connections':
{'quick_id':
{'$in':['conn_id_2', 'conn_id_3']}
}
}
}
)
What am I missing?
Thanks!