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: Backlog
    • 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
        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.
        Hide
        tony-kerz tony kerz added a comment -

        i just came up against this, and it is resulting in a non-trivial amount of effort to enforce this at the application level. gotta say, the fact that there are almost 60 votes for this issue, half a dozen stack overflow type posts, and it has been sitting here for 7 years after Eliot stated "its mostly clear its a bug", kind of makes me sad.

        Show
        tony-kerz tony kerz added a comment - i just came up against this, and it is resulting in a non-trivial amount of effort to enforce this at the application level. gotta say, the fact that there are almost 60 votes for this issue, half a dozen stack overflow type posts, and it has been sitting here for 7 years after Eliot stated "its mostly clear its a bug", kind of makes me sad.

          People

          • Votes:
            63 Vote for this issue
            Watchers:
            63 Start watching this issue

            Dates

            • Created:
              Updated: