0

I'm attempting to set a unique index on a field within a subarray of a collection in MongoDB. This index will also be set to drop existing dupes. I'm following the MongoDB documentation, but it does not seem to be working (dupes still exist in my db). I believe my issue may be how I'm attempting to access the document within the 'photos' sub-array. Below is my schema and the code I'm trying to use to set the index:

{
    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 } )
MattDionis
  • 3,534
  • 10
  • 51
  • 105
  • A unique index on a sub-doc field within an array prevents dups between docs but not within them. Is that what you're seeing? – JohnnyHK Feb 22 '15 at 14:19
  • Thanks, yes that's what I'm seeing. I'm trying to prevent dupes within that 'photos' subarray. – MattDionis Feb 22 '15 at 14:21
  • See the second part of this answer: http://stackoverflow.com/questions/15921700/mongoose-unique-values-in-nested-array-of-objects/15926031#15926031 – JohnnyHK Feb 22 '15 at 14:23

0 Answers0