[DOCS-2151] Comment on: "manual/core/index-sparse.txt" Created: 25/Oct/13  Updated: 03/Nov/17  Resolved: 25/Oct/13

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

Type: Improvement Priority: Major - P3
Reporter: Kay Agahd Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

v2.4.6 Linux 64 bit

Location: http://docs.mongodb.org/manual/core/index-sparse/
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0 IKDhPmJcdw
Referrer: http://docs.mongodb.org/manual/core/indexes/
Screen Resolution: 1680 x 1050
repo: docs
source: core/index-sparse


Participants:
Days since reply: 10 years, 16 weeks, 5 days ago

 Description   

Your example of the unique sparse index is wrong. Why shouldn't one be able to insert the last two records? Unique index is on score and not on userid!
Here my console output to prove it:
mongos> db.scores.find()
mongos> db.scores.ensureIndex(

{ score: 1 }

,

{ sparse: true, unique: true }

)
mongos> db.scores.insert(

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

)
mongos> db.scores.insert(

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

)
mongos> db.scores.insert(

{ "userid": "nuZHu2tcRm" }

)
mongos> db.scores.insert(

{ "userid": "HIGvEZfdc5" }

)
mongos> db.scores.find()

{ "_id" : ObjectId("526a8be3f46d2610cd367984"), "userid" : "PWWfO8lFs1", "score" : "43" } { "_id" : ObjectId("526a8be3f46d2610cd367985"), "userid" : "XlSOX66gEy", "score" : "34" } { "_id" : ObjectId("526a8be3f46d2610cd367986"), "userid" : "nuZHu2tcRm" } { "_id" : ObjectId("526a8be5f46d2610cd367987"), "userid" : "HIGvEZfdc5" }

mongos> db.scores.insert(

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

)
mongos> db.scores.insert(

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

)
mongos> db.scores.find()

{ "_id" : ObjectId("526a8be3f46d2610cd367984"), "userid" : "PWWfO8lFs1", "score" : "43" } { "_id" : ObjectId("526a8be3f46d2610cd367985"), "userid" : "XlSOX66gEy", "score" : "34" } { "_id" : ObjectId("526a8be3f46d2610cd367986"), "userid" : "nuZHu2tcRm" } { "_id" : ObjectId("526a8be5f46d2610cd367987"), "userid" : "HIGvEZfdc5" } { "_id" : ObjectId("526a8bf5f46d2610cd367988"), "userid" : "PWWfO8lFs1", "score" : "82" } { "_id" : ObjectId("526a8bf7f46d2610cd367989"), "userid" : "XlSOX66gEy", "score" : "90" }

mongos>



 Comments   
Comment by Kay Kim (Inactive) [ 25/Oct/13 ]

Hey Agahd – made the changes (although not yet published). Once published, the pages should reflect the correct example.
Thanks again for all your time and effort with this. Much appreciated.

Regards,

Kay Kim

Comment by auto [ 25/Oct/13 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2151 fix syntax for sparse index
Branch: v2.4
https://github.com/mongodb/docs/commit/96fd37ec0e57365416cafcd897ebca40f84f2292

Comment by auto [ 25/Oct/13 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2151 fix syntax for sparse index
Branch: master
https://github.com/mongodb/docs/commit/9afc1bc694e80b1735f7dd09341b1a0a0b87c4c2

Comment by Kay Agahd [ 25/Oct/13 ]

Wow, how fast are you, great!
Yes, we're just cross posting....

Comment by Kay Kim (Inactive) [ 25/Oct/13 ]

Hi Agahd –
clearly we're on the same wavelength : ) And I too forgot about the formatting. Yikes.

Comment by Kay Agahd [ 25/Oct/13 ]

I'm sorry about the formatting. I didn't find a way to edit my initial post.

Furthermore, I only saw right now that your example pretends to have already this two documents:

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

So I understand why you said that the index should not permit adding the last two documents:

db.scores.insert( { "userid": "PWWfO8lFs1", "score": "82" } )
db.scores.insert( { "userid": "XlSOX66gEy", "score": "90" } )

However, this is false nonetheless because the type of score is different (String vs. Number).

Steps to reproduce:

mongos> db.scores.remove()
mongos> db.scores.insert({ "_id" : ObjectId("523b6e32fb408eea0eec2647"), "userid" : "newbie" })
mongos> db.scores.insert({ "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 })
mongos> db.scores.insert({ "_id" : ObjectId("523b6e6ffb408eea0eec2649"), "userid" : "nina", "score" : 90 })
mongos> db.scores.ensureIndex( { score: 1 } , { sparse: true, unique: true } )
mongos> db.scores.insert( { "userid": "PWWfO8lFs1", "score": "43" } )
mongos> db.scores.insert( { "userid": "XlSOX66gEy", "score": "34" } )
mongos> db.scores.insert( { "userid": "nuZHu2tcRm" } )
mongos> db.scores.insert( { "userid": "HIGvEZfdc5" } )
mongos> db.scores.insert( { "userid": "PWWfO8lFs1", "score": "82" } )
mongos> db.scores.insert( { "userid": "XlSOX66gEy", "score": "90" } )
mongos> db.scores.find()
{ "_id" : ObjectId("523b6e32fb408eea0eec2647"), "userid" : "newbie" }
{ "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 }
{ "_id" : ObjectId("523b6e6ffb408eea0eec2649"), "userid" : "nina", "score" : 90 }
{ "_id" : ObjectId("526a8ee7f46d2610cd36798c"), "userid" : "PWWfO8lFs1", "score" : "43" }
{ "_id" : ObjectId("526a8ee7f46d2610cd36798d"), "userid" : "XlSOX66gEy", "score" : "34" }
{ "_id" : ObjectId("526a8ee7f46d2610cd36798e"), "userid" : "nuZHu2tcRm" }
{ "_id" : ObjectId("526a8ee8f46d2610cd36798f"), "userid" : "HIGvEZfdc5" }
{ "_id" : ObjectId("526a8eebf46d2610cd367990"), "userid" : "PWWfO8lFs1", "score" : "82" }
{ "_id" : ObjectId("526a8eecf46d2610cd367991"), "userid" : "XlSOX66gEy", "score" : "90" }

Comment by Kay Kim (Inactive) [ 25/Oct/13 ]

Hi Agahd -
thanks for your comment. The reason that the unique index would prevent the insertion of the last two records is that the collection initially contains:

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

However, the example incorrectly has enclosed the scores 82 and 90 in quotes, so in essence, "82" would not equal 82 and "90" would not equal 90 and thus the index would still allow these inserts. Will correct the example

db.scores.insert(

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

)
db.scores.insert(

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

)

Will update this so that the two inserts that would fail would be:

db.scores.insert(

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

)
db.scores.insert(

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

)

Thanks again.

Kay

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