用pymong实现ensureIndex()删除重复文档

2024-09-27 17:39:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要删除基于字段“X”的唯一值的重复条目。它在mongodb shell中运行良好:

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

我想用pymongo运行它,我使用的代码如下:

^{pr2}$

此代码引发:

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

我哪里出错了?在


Tags: 代码truedbifmongodb条目shelllist
1条回答
网友
1楼 · 发布于 2024-09-27 17:39:19

在几个方面,pymongo调用的语法错误。假设“collection”是集合的名称,这应该可以工作。在

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

Pymongo(和Python)需要一个键方向对列表,而不是Dict,因为Dict是无序的。升序索引的语法也是pymongo.升序. 最后,unique和dropDups不需要“$”。在

相关问题 更多 >

    热门问题