[DOCS-7579] Comment on: "manual/core/index-sparse.txt" Created: 05/Apr/16  Updated: 03/Nov/17  Resolved: 05/Apr/16

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Bug Priority: Major - P3
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Location: https://docs.mongodb.org/v2.4/core/index-sparse/#index-type-sparse
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Referrer: https://docs.mongodb.org/v2.4/core/index-unique/
Screen Resolution: 2560 x 1440
repo: docs
source: core/index-sparse


Participants:
Days since reply: 7 years, 45 weeks, 1 day ago

 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 }

)
```



 Comments   
Comment by Allison Reinheimer Moore [ 05/Apr/16 ]

In that example, we first say:

"Consider a collection scores that contains the following documents:

{ "_id" : ObjectId("523b6e32fb408eea0eec2647"), "userid" : "newbie" }
{ "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 }
{ "_id" : ObjectId("523b6e6ffb408eea0eec2649"), "userid" : "nina", "score" : 90 }

and then add the unique index on the score field.

When we then try to insert the two documents you reference, they'd match the existing documents in the collection. Sorry for the confusion!

Generated at Thu Feb 08 07:54:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.