mongodbmergeaggregateunique-index

mongoDb:Cannot find index to verify that join fields will be unique


Hey I need to merge my two collections,but mongo retuns "Cannot find index to verify that join fields will be unique"

1.create unique index 2.remove 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" 
    }}
])

expected:retuns GeoLite2-City-Blocks-IPv4 include GeoLite2-City-Locations-ja


Solution

  • The $merge aggregation stage requires a unique index on the identifier fields:

    $merge requires a unique, index with keys that correspond to the on identifier fields. Although the order of the index key specification does not matter, the unique index must only contain the on fields as its keys.

    For your operation, there would need to exist a unique index on the GeoLite2-City-Locations-ja collection that contains the field geoname_id.