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

Unique index on array keys only

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Do
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • None
    • Query

      As far as I can tell this is not possible with the current unique index, but it would be great to be able to unique-index just the keys of an array. This would really help in de-normalization as the following example could act as both a user table and products table if the key of the products array were unique indexed so that keys are unique between all documents in the table.

      array(
      '_id' => 1,
      'name' => 'Rita',
      'userdata' => array(...),
      'products' => array(
      1 => array('name' => 'cube', 'style' => 'polka dots'),
      2 => array('name' => 'tiles', 'style' => 'checkerboard')
      )
      )

      array(
      '_id' => 2,
      'name' => 'Joe',
      'userdata' => array(...),
      'products' => array(
      3 => array('name' => 'circle', 'style' => 'round'),
      4 => array('name' => 'triangle', 'style' => '3-sided')
      )
      )

      With the current Mongo unique index I could still add the following even though the key already exists within another document:

      array(
      '_id' => 3,
      'products' => array(
      3 => array('name' => 'duplicate key', 'style' => 'key is not unique...')
      )
      )

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            phpmoadmin phpMoAdmin
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: