Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1068

unique indexes not enforced within array of single document

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: 1.4.0
    • Fix Version/s: Planning Bucket A
    • Component/s: Indexing
    • Labels:
      None
    • Environment:
      linux

      Description

      I am finding that a unique index is not applied to multiple elements of an array in the same document of a collection. They are only applied between documents in a collection.

      More details available at: http://groups.google.com/group/mongodb-user/browse_thread/thread/56bf4f84f7cad835

      Is this intended behavior? Is there a way around it?

        Activity

        Hide
        ethangarofolo Ethan Garofolo added a comment -

        +1 for the issue and for either of Joe's solutions. Those both feel very natural.

        Show
        ethangarofolo Ethan Garofolo added a comment - +1 for the issue and for either of Joe's solutions. Those both feel very natural.
        Hide
        dhubler Douglas Hubler added a comment -

        I was able to use a workaround, you can "normalize" the data

        $db.bar.find()
        {_id: 10, foo: 1, b:1}
        {_id: 11, foo: 1, b:2}
        {_id: 12, foo: 1, b:3}

        $db.foo.find()
        {_id: 1, bar:[10,11,12]}

        Then you can

        • use $addToSet on foo.bar to manage unique items
        • add compound key bar {foo,b}

          to restrict duplicates

        • use upsert on bar collection to easily add new items or update existing ones. trick is to read the return from the upsert use that in the $addToSet operation on foo
        Show
        dhubler Douglas Hubler added a comment - I was able to use a workaround, you can "normalize" the data $db.bar.find() {_id: 10, foo: 1, b:1} {_id: 11, foo: 1, b:2} {_id: 12, foo: 1, b:3} $db.foo.find() {_id: 1, bar: [10,11,12] } Then you can use $addToSet on foo.bar to manage unique items add compound key bar {foo,b} to restrict duplicates use upsert on bar collection to easily add new items or update existing ones. trick is to read the return from the upsert use that in the $addToSet operation on foo
        Hide
        psi29a Bret Curtis added a comment -

        Any movement on this?

        Show
        psi29a Bret Curtis added a comment - Any movement on this?
        Hide
        bawongfai Simon Ho added a comment -

        Is there any update for this bug?

        Show
        bawongfai Simon Ho added a comment - Is there any update for this bug?
        Hide
        cameron.stitt@simplehq.co Cameron Stitt added a comment -

        Again, any movement on this? I will have to re-architect my solution to go across multiple collections (which would then argue why use mongo) rather than have the ability for intra-document uniqueness.

        Show
        cameron.stitt@simplehq.co Cameron Stitt added a comment - Again, any movement on this? I will have to re-architect my solution to go across multiple collections (which would then argue why use mongo) rather than have the ability for intra-document uniqueness.

          People

          • Votes:
            57 Vote for this issue
            Watchers:
            54 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since reply:
              7 weeks, 3 days ago
              Date of 1st Reply: