2d compound index

57 views
Skip to first unread message

Andi

unread,
Jan 15, 2011, 7:58:19 AM1/15/11
to mongodb-user
Some examples:

db.test.insert({
point:[1,10],
tags:[
{k:'key',v:'value'},
{k:'key2',v:123}
]
})

db.test.insert({
point:[1,10],
tags:{k:'key',v:'value'}
})

db.test.dropIndexes()
db.test.ensureIndex({point:"2d"})
db.test.find({
point:{"$within":{"$box":[[0,0],[12,12]]}},
"tags.k": 'key'
})

// 2 results

db.test.dropIndexes()
db.test.ensureIndex({point:"2d","tags.k":1,"tags.v":1})

db.test.find({
point:{"$within":{"$box":[[0,0],[12,12]]}},
"tags.k":"key"
})

// no results

db.test.dropIndexes()
db.test.ensureIndex({point:"2d","tags.k":1})

db.test.find({
point:{"$within":{"$box":[[0,0],[12,12]]}},
"tags.k":"key"
})

// no results

Why do the last two queries do not return any results?

Nat

unread,
Jan 15, 2011, 8:22:25 AM1/15/11
to mongodb-user
I believe this is fixed in 1.7.x branch.

Andi

unread,
Jan 15, 2011, 9:45:28 AM1/15/11
to mongodb-user
Yes, it works. But I am wondering about the explain() output:

{
"cursor" : "GeoBrowse-box",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {

}
}

This looks like no index is used.

Eliot Horowitz

unread,
Jan 15, 2011, 10:39:37 AM1/15/11
to mongod...@googlegroups.com
 "cursor" : "GeoBrowse-box"
means that the geo index was used.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Andi

unread,
Jan 15, 2011, 11:15:00 AM1/15/11
to mongodb-user
And the other parts of the index - are they included? Usually this is
reflected in indexBounds.

Eliot Horowitz

unread,
Jan 15, 2011, 11:16:56 AM1/15/11
to mongod...@googlegroups.com
The explain for geo is a bit week, so its not included.
Case is here: http://jira.mongodb.org/browse/SERVER-1429
It should be using the index to resolve those though.

Andi

unread,
Jan 19, 2011, 2:52:25 PM1/19/11
to mongodb-user
My answer wasn't clear enough ...

2 experiments with unexpected results in db.version() 1.7.4


1)

db.test.insert({
point:[1,10],
tags:[
{k:'key',v:'value'},
{k:'key2',v:123}
]
})

db.test.insert({
point:[1,10],
tags:{k:'key',v:'value'}
})

db.test.ensureIndex({point:"2d","tags.k":1,"tags.v":1})
db.test.find({
point:{"$within":{"$box":[[0,0],[12,12]]}},
"tags.k":"key"
})

Result:

{ "_id" : ObjectId("4d373f753ffe71e78a71bfc0"), "point" : [ 1, 10 ],
"tags" : { "k" : "key", "v" : "value" } }

Why is only 1 object returned?


2)
db.test.drop()
db.test.insert({p:[1112,3473],t:[{k:'a',v:'b'},{k:'c',v:'d'}]})
db.test.ensureIndex({p:'2d','t.k':1,'t.v':1},{min:0,max:10000})
db.test.find({p:[1112,3473],'t.k':'a'})

No result.

Andi

unread,
Jan 19, 2011, 7:44:36 PM1/19/11
to mongodb-user
http://jira.mongodb.org/browse/SERVER-2381
I couldn't find it in issues, so I have added it.
Reply all
Reply to author
Forward
0 new messages