mongoDb:找不到索引来验证连接字段是否唯一

Mik*_*how 2 merge aggregate unique-index mongodb

嘿,我需要合并我的两个集合,但 mongo 返回“无法找到索引来验证连接字段是否唯一”

1.创建唯一索引 2.删除whenMatched/whenNotMatched

db.getCollection("GeoLite2-City-Blocks-IPv4").aggregate([
    {$match:{longitude:{$gt:175,$lt:180}}},
    {$merge:{
        into:"GeoLite2-City-Locations-ja",
        on:"geoname_id",
        whenMatched: "replace",
        whenNotMatched: "insert" 
    }}
])
Run Code Online (Sandbox Code Playgroud)

预期:返回 GeoLite2-City-Blocks-IPv4 包括 GeoLite2-City-Locations-ja

Ada*_*son 7

$merge 聚合阶段需要标识符字段上的唯一索引

$merge 需要一个唯一的索引,其键对应于标识符字段。尽管索引键规范的顺序并不重要,但唯一索引必须仅包含 on 字段作为其键。

GeoLite2-City-Locations-ja对于您的操作,包含该字段的集合上需要存在唯一索引geoname_id

  • 你好,Adam,geoLite2-City-Locations-ja 和 geoLite2-City-Blocks-IPv4 集合都有一个包含 geoname_id 的唯一索引 (2认同)