[DOCS-3226] Mention how sparse indexing works with compound and geospatial indexes Created: 21/Apr/14  Updated: 02/Jun/14  Resolved: 02/Jun/14

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: v1.3.6

Type: Improvement Priority: Minor - P4
Reporter: Jeremy Mikola Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 9 years, 37 weeks, 2 days ago

 Description   

The following pages refer to sparse indexes:

I came across old documentation in the PHP driver stating that sparse indexes only worked with single-field indexes. Thinking that was incorrect, I decided to try them with compound indexes and found that they will index any documents with at least one of the included fields:

> db.foo.drop()
true
> db.foo.ensureIndex({x:1,y:1},{sparse:true})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.foo.insert({x:1})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({y:1})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({x:2,y:2})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({z:1})
WriteResult({ "nInserted" : 1 })
> db.foo.find().hint("x_1_y_1")
{ "_id" : ObjectId("535532a190f51aaf07927fbb"), "y" : 1 }
{ "_id" : ObjectId("5355329f90f51aaf07927fba"), "x" : 1 }
{ "_id" : ObjectId("535532a590f51aaf07927fbc"), "x" : 2, "y" : 2 }

Also, our current documentation says that sparse only works with asc/desc indexes. It appears to work with geospatial indexes as well.

> db.foo.drop()
true
> db.foo.ensureIndex({loc:"2d"},{sparse:true})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.foo.insert({x:1})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({loc:[0,0]})
WriteResult({ "nInserted" : 1 })
> db.foo.find({loc:{$near:[0,0]}})
{ "_id" : ObjectId("5355322e90f51aaf07927fb9"), "loc" : [ 0, 0 ] }

I didn't test text indexes, but that would be another thing to confirm.



 Comments   
Comment by Githook User [ 02/Jun/14 ]

Author:

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

Message: DOCS-3226 sparse property of compound indexes
Branch: master
https://github.com/mongodb/docs/commit/4c8f77127aac3acdf617f598d6b1c2368bc31d76

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