Core Server
  1. Core Server
  2. SERVER-1068

unique indexes not enforced within array of single document

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major - P3 Major - P3
    • Resolution: Unresolved
    • Affects Version/s: 1.4.0
    • Fix Version/s: Planning Bucket A
    • Component/s: Shell
    • Labels:
      None
    • Environment:
      linux
    • Backport:
      No
    • # Replies:
      10
    • Last comment by Customer:
      true

      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
        Tzury Bar Yochay
        added a comment -

        Wasn't aware of this ticket, there is a small thread on this at stack-overflow.

        http://stackoverflow.com/questions/9390590/mongodb-indexing-embedded-fields-dot-notation

        Show
        Tzury Bar Yochay
        added a comment - Wasn't aware of this ticket, there is a small thread on this at stack-overflow. http://stackoverflow.com/questions/9390590/mongodb-indexing-embedded-fields-dot-notation
        Hide
        A. Jesse Jiryu Davis
        added a comment -

        Also, it seems this should fail but doesn't:

        db.foo.insert({a:[

        {b:1}

        ,

        {b:1}

        ]})

        Show
        A. Jesse Jiryu Davis
        added a comment - Also, it seems this should fail but doesn't: db.foo.insert({a:[ {b:1} , {b:1} ]})
        Hide
        Joe Gornick
        added a comment -

        I wrote a post about unique indexes @ http://joegornick.com/2012/10/25/mongodb-unique-indexes-on-single-embedded-documents/

        I also have some proposed solutions for declaring single document unique indexes and would love to get some feedback!

        Show
        Joe Gornick
        added a comment - I wrote a post about unique indexes @ http://joegornick.com/2012/10/25/mongodb-unique-indexes-on-single-embedded-documents/ I also have some proposed solutions for declaring single document unique indexes and would love to get some feedback!
        Hide
        Ethan Garofolo
        added a comment -

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

        Show
        Ethan Garofolo
        added a comment - +1 for the issue and for either of Joe's solutions. Those both feel very natural.
        Hide
        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
        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

          People

          • Votes:
            44 Vote for this issue
            Watchers:
            38 Start watching this issue

            Dates

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