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

Unique sparse index with array field - can't have two docs with empty arrays (new in v2.0)

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.0.0
    • Component/s: Index Maintenance
    • None
    • Query
    • ALL

      This seems to be a change in behaviour between 1.8 and 2.0:

      With v1.8.1:

      MongoDB shell version: 1.8.1

      connecting to: odyssey_test
      > use foo_test
      switched to db foo_test
      > db.users.ensureIndex({aliases:1},{sparse:true,unique:true})
      > db.users.insert({name:"alice", aliases:[]})
      > db.users.insert({name:"bob", aliases:[]})
      > db.users.find()
      { "_id" : ObjectId("4e7b5b219521a5c75ce16608"), "name" : "alice", "aliases" : [ ] }
      { "_id" : ObjectId("4e7b5b289521a5c75ce16609"), "name" : "bob", "aliases" : [ ] }
      

      with v2.0.0:

      MongoDB shell version: 2.0.0

      connecting to: test
      > use foo_test
      switched to db foo_test
      > db.users.ensureIndex({aliases:1},{sparse:true,unique:true})
      > db.users.insert({name:"alice", aliases:[]})
      > db.users.insert({name:"bob", aliases:[]})
      E11000 duplicate key error index: foo_test.users.$aliases_1  dup key: { : undefined }
      

      If I instead create the index with v:0 (to force old-style, pre-v2.0 index type), I don't get the dup key error, i.e. same behaviour as 1.8 (as you would expect).

      Is this intended? I don't see anything in release notes...

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            bensymonds Ben Symonds
            Votes:
            4 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: