Search code examples
mongodbloopbacknosql

E11000 duplicate key error index - Removing auto generated ID from mlab


How can I get mlab/mongodb to stop autogenerating one of these IDs?

key: { : ObjectId('584f7e76fd51ea874cee3c70') }",
"op":{"title":"Gold (feat. Lil Wayne) (Remix)",
"artist":"kiiara",
"audio":"https://api.soundcloud.com/tracks/293663905/stream?client_id=90d140308348273b897fab79f44a7c89",
"image":"https://i1.sndcdn.com/artworks-C09aBfgKDpCI-0-t500x500.jpg",
"download":"https://api.soundcloud.com/tracks/293663905/download",
"url":"https://soundcloud.com/kiiaraonline/gold-lil-wayne-remix",
"created":"2016-12-13T04:52:06.309Z",
"genres":{"hipHop":30,"house":20,"pop":50},
"_id":"584f7e76fd51ea874cee3c70"}

When I'm trying to create a relationship I get an error saying there are two keys. And I think that is because there are two IDs. How can I remove one of them? Preferably the ObjectId one.

The strange thing is, when I view them in the database I only see this

{
    "_id": {
        "$oid": "584f7e76fd51ea874cee3c70"
    },
    "title": "Gold (feat. Lil Wayne) (Remix)",
    "artist": "kiiara",
    "audio": "https://api.soundcloud.com/tracks/293663905/stream?client_id=90d140308348273b897fab79f44a7c89",
    "image": "https://i1.sndcdn.com/artworks-C09aBfgKDpCI-0-t500x500.jpg",
    "download": "https://api.soundcloud.com/tracks/293663905/download",
    "url": "https://soundcloud.com/kiiaraonline/gold-lil-wayne-remix",
    "created": {
        "$date": "2016-12-13T04:52:06.309Z"
    },
    "genres": {
        "hipHop": 30,
        "house": 20,
        "pop": 50
    }
}

Thank you


Solution

  • Use $exists in update as below :

    db.collectioName.update({"key":{"$exists":true}},{"$unset":{"key":""}},
                                   {"upsert":false,"multi":true})
    

    unset remove key fields in all matching documents .