Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-7579

Comment on: "manual/core/index-sparse.txt"

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 01112017-cleanup
    • None
    • None

    Description

      The final statement on this page appears to be wrong:

      ```
      However, this index would not permit adding the following documents:

      db.scores.insert(

      { "userid": "PWWfO8lFs1", "score": 82 }

      )
      db.scores.insert(

      { "userid": "XlSOX66gEy", "score": 90 }

      )
      ```

      It would allow those documents because their score value is unique. I think you accidentally set the userid's to be similar to past inserted data rather than the scores. I tested this simply by entering the commands:

      ```
      > db.scores.ensureIndex(

      { score: 1 }

      ,

      { sparse: true, unique: true }

      )

      { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }

      > db.scores.insert(

      { "userid": "PWWfO8lFs1", "score": 43 }

      )
      WriteResult(

      { "nInserted" : 1 }

      )
      > db.scores.insert(

      { "userid": "XlSOX66gEy", "score": 34 }

      )
      WriteResult(

      { "nInserted" : 1 }

      )
      > db.scores.insert(

      { "userid": "nuZHu2tcRm" }

      )
      WriteResult(

      { "nInserted" : 1 }

      )
      > db.scores.insert(

      { "userid": "HIGvEZfdc5" }

      )
      WriteResult(

      { "nInserted" : 1 }

      )
      > db.scores.insert(

      { "userid": "PWWfO8lFs1", "score": 82 }

      )
      WriteResult(

      { "nInserted" : 1 }

      )
      ```

      Attachments

        Activity

          People

            Unassigned Unassigned
            xgen-internal-docs Docs Collector User (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              7 years, 45 weeks, 1 day ago