E11000重复键错误索引 - 从mlab中删除自动生成的ID

时间:2016-12-13 05:06:41

标签: mongodb loopback nosql

如何让mlab / mongodb停止自动生成其中一个ID?

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"}

当我试图创建一个关系时,我得到一个错误,说有两个键。我认为这是因为有两个ID。我怎样才能删除其中一个?优选地,ObjectId是一个。

奇怪的是,当我在数据库中查看它们时,我只看到了这个

{
    "_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
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

在更新中使用$exists,如下所示:

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

unset删除所有匹配文档中的key个字段。