$pull giving duplicate key error

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!

Hello @aleph-0 ,

Welcome to The MongoDB Community Forums! :wave:

I notice you haven’t had a response to this topic yet, were you able to find a solution?

The error message specifically mentions the index “connections.connection_unique_name_1_connections.data.id_1” and the duplicate key values of “{ connections.connection_unique_name: null, connections.data.id: null }”. This means that you are trying to update a document with a “connections.connection_unique_name” field value of “null” and a “connections.data.id” field value of “null” into a collection that has a unique index defined on these two fields, and a document with these exact field values already exists in the collection.

When I tried your query with the document you provided, it was working as expected.

I believe that in your scenario, you have documents as below:

  • First example document
{
    'asset_name': 'asset1',
    'asset_unique_name': 'asset1_unique',
    'connections': [
    ]
}
  • Second document example
{
    'asset_name': 'asset1',
    'asset_unique_name': 'asset1_unique',
    'connections': [
        {
            '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'
            }
        }
    ]
}

And when you ran your query, it tried to update second example document but gave that respective error as two documents cannot have same values(means both the fields are missing hence it is showing it’s value as null).

Could you please confirm if such cases exist in your collection?

Regards,
Tarun

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.