pymongo create and ensure index

1,769 views
Skip to first unread message

grahamhar

unread,
Sep 30, 2012, 9:21:57 AM9/30/12
to mongod...@googlegroups.com
Hi,

When performing create_index or ensure_index on a collection via python driver the default behaviour is to make the index unique this is the oposite of the behaviour of the shell. Should this be changed or at least the defaults for these should be updated in the documentation (these settings are optional but seem to be applied with a default of True for unique)?

Regards
Graham

Bernie Hackett

unread,
Sep 30, 2012, 10:47:59 AM9/30/12
to mongod...@googlegroups.com
The default is definitely not to make the index unique. Why do you think it is?

Sent from my iPhone
--
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
See also the IRC channel -- freenode.net#mongodb

grahamhar

unread,
Sep 30, 2012, 12:41:11 PM9/30/12
to mongod...@googlegroups.com
Hi,

So I had a mistake in my code which I have corrected I was calling create_index('a',-1) instead of with a list of tuples create_index([('a',-1)]) seems the 1 was getting interpreted as True for the unique option.

Bernie Hackett

unread,
Sep 30, 2012, 1:20:19 PM9/30/12
to mongod...@googlegroups.com
That doesn't create a unique index either, just an ascending index on
'a' with a cache time of -1 seconds (effectively, cache for 0
seconds).

>>> db.test.create_index('a', -1)
u'a_1'
>>> pprint.pprint(db.test.index_information())
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
u'a_1': {u'key': [(u'a', 1)], u'v': 1}}

The first parameter is key_or_list, the second is cache_for (or `ttl`
in older pymongo versions).

grahamhar

unread,
Sep 30, 2012, 3:26:35 PM9/30/12
to mongod...@googlegroups.com
Hi,

I pasted a test I did with -1 if you use 1 I see the following:

>>> dbn['test'].ensure_index('a',1)
u'a_1'
>>> print dbn['test'].index_information()
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1}, u'a_1': {u'unique': True, u'key': [(u'a', 1)], u'v': 1}}
>>> 

and from mongo shell:
> db.test.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "graham.test",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"a" : 1
},
"unique" : true,
"ns" : "graham.test",
"name" : "a_1"
}
]

Bernie Hackett

unread,
Sep 30, 2012, 6:30:02 PM9/30/12
to mongod...@googlegroups.com
In that case you're just caching the index for 1 second. Here's what I
get with your code:

>>> dbn = c.dbn
>>> dbn['test'].ensure_index('a',1)
u'a_1'
>>> pprint.pprint(dbn['test'].index_information())
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
u'a_1': {u'key': [(u'a', 1)], u'v': 1}}

You can read the source here:

https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/collection.py#L746-832

You have to pass unique=True to create a unique index. Are you sure
you didn't previously create a unique index? What version of PyMongo
and MongoDB are you using?

grahamhar

unread,
Oct 1, 2012, 3:41:10 AM10/1/12
to mongod...@googlegroups.com
Hi,

I was on:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import pymongo

>>> pymongo.version

'2.1.1'

upgraded to:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import pymongo

>>> pymongo.version

'2.3'

and I now see same behaviour as you, so it looks like the issue was in pymongo 2.1.1


Thanks

Graham

Reply all
Reply to author
Forward
0 new messages