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)

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Minor - P4
    • Resolution: Gone away
    • 2.0.0
    • None
    • Index Maintenance
    • None
    • ALL

    Description

      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...

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: