0

I'm trying to install this software, a few scripts for searching in a local mongodb database: https://github.com/wimremes/cve-search/blob/master/README.md

Since the search.py script fail miserably, I'm trying to put some index as suggested:

db.cpe.ensureIndex( {id:1 } )
db.cves.ensureIndex( {id:1} )
db.cves.ensureIndex( {vulnerable_configuration:1} )
db.vfeed.ensureIndex( {id:1} )

But this code is not working: ensureIndex fail with this error:

File "build/bdist.linux-i686/egg/pymongo/collection.py", line 1672, in __call__
TypeError: 'Collection' object is not callable. If you meant to call the 'ensureIndex' method on a 'Collection' object it is failing because no such method exists.

and ensure_index( {id:1}) return this:

TypeError: if no direction is specified, key_or_list must be an instance of list

What shoud I do for ceating new indexes?

5
  • db.cpe.create_index( "id") is working, but i don't know if has the same effect.
    – asdf
    Feb 15, 2014 at 16:08
  • what driver are you using? pymongo?
    – joao
    Feb 15, 2014 at 16:16
  • yes! the driver used in the cve-search software.
    – asdf
    Feb 15, 2014 at 16:17
  • I'm not into python/pymongo, but there is already a topic here featuring create_index vs ensure_index stackoverflow.com/questions/5912661/…
    – joao
    Feb 15, 2014 at 16:20
  • Yes i know perfectly the difference thanks to that post. :) This is matter of parameters, i guess.
    – asdf
    Feb 15, 2014 at 16:45

2 Answers 2

8

Actually, in PyMongo they would look like this:

db.collection.ensure_index([("id", pymongo.ASCENDING)])

You can read more in the PyMongo Documentation.


Update: In version 3.0 and above ensure_index has been removed. You should now use create_index instead. For more info, see the docs for the JavaScript library.

You can also read about ensure_index vs create_index here: Pymongo / MongoDB: create index or ensure index?

3
  • The documentation links to pymongo 3.0 . Does it work with 3.2 . Its not working for me. Jan 5, 2016 at 12:33
  • @AkshayHazari It seems that ensure_index was deprecated (and removed) past or in 3.0, I do not know which is the appropriate way to perform the action past 3.0. Perhaps try just using the create_index method instead?
    – erb
    Jan 5, 2016 at 12:38
  • It seems that in the JavaScript API ensureIndex is just an alias for createIndex since version 3.0: docs.mongodb.org/v3.0/reference/method/…
    – erb
    Jan 5, 2016 at 12:40
2

What you've written is in Javascript syntax. In Python using PyMongo, the same operations look like:

db.cpe.ensure_index( {'id':1 } )
db.cves.ensure_index( {'id':1} )
db.cves.ensure_index( {'vulnerable_configuration':1} )
db.vfeed.ensure_index( {'id':1} )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.