Question

I need to delete the duplicate entries based on unique values of field "X". It works fine in mongodb shell:

db.collection.ensureIndex({x' : 1},{unique: true, dropDups: true})

I want to run this using pymongo and the code I used is as follows:

import pymongo
import mmh3

conn = pymongo.MongoClient()
db = conn['scrapy-mongodb']

db.collection.ensure_index({'x' : 1}, {'$unique' : True, '$dropDups' : True})

This code throws:

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

Where did I go wrong?

Was it helpful?

Solution

You've got the wrong syntax for a pymongo call in a couple of areas. This should work assuming "collection" is the name of the collection.

db.collection.ensure_index([("x" , pymongo.ASCENDING), ("unique" , True), ("dropDups" , True)])

Pymongo (and Python) require a list of key direction pairs not a Dict, as Dicts are unordered. Also the syntax for an ascending index is pymongo.ASCENDING. Finally no "$" required for unique and dropDups.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top