如何在子数组(MongoDB)中设置文档索引

时间:2015-02-22 14:03:26

标签: javascript arrays mongodb mongoose

我正在尝试在MongoDB中的集合的子数组中的字段上设置唯一索引。该指数也将设置为删除现有的欺骗。我正在关注MongoDB documentation,但它似乎没有工作(我的数据库中仍然存在欺骗)。我相信我的问题可能是我试图访问'photos'子阵列中的文档。下面是我的架构和我试图用来设置索引的代码:

{
    event: String,
    headline: String,
    city: String,
    state: String,
    date: String,
    start: String,
    end: String,
    dateState: String,
    radius: String,
    team_1: String,
    team_2: String,
    object_id: String,
    longitude: String,
    latitude: String,
    cover: {
        img: String,
        username: String
    },
    photos: [{
        img: String,
        link: String,
        username: String,
        profile: String,
        text: String,
        longitude: String,
        latitude: String,
        created_time: String
    }]
}

gameday.events.ensureIndex( {"photos.img": 1}, { unique: true, dropDups: true } )

0 个答案:

没有答案