How to stop duplications on Cockpit?

I am adding Urls with Title and Description of a website.
In future, I will add more links to Collections(entries). but I am facing issues with duplicate entries!

how to get rid of it? and in future when I add more entries, is it possible that when I change the Description and import so it will update and don’t create duplicate values?

you can throw an error before saving:

(snippet should be located in config/bootstrap.php)

$app->on('', function($name, $entry, $isUpdate) {

    $fields = ['title', 'description'];

    foreach ($fields as $field) {

        if (!isset($entry[$field]) || !$entry[$field]) continue;

        $filter = [];
        $filter[$field] = $entry[$field];

        if ($isUpdate && isset($entry['_id'])) {
            $filter['_id'] = ['$not' => $entry['_id']];

        $check = $this->module('collections')->findOne($name, $filter);

        if ($check) {
            $this->stop(['error' => "{$field} must be unique"], 412);

Replace MyCollection with your collection name

1 Like

Thanks @artur, that can be quite useful, I added it as a configurable option in the helpers addon -

Btw, the $not seems not to work with the mongodb driver, it expects a regex! I used the below:

        if ($this->app->storage->type === 'mongodb') {
          $filter['_id'] = ['$ne' => new MongoDB\BSON\ObjectId($entry['_id'])];
        else {
          $filter['_id'] = ['$not' => $entry['_id']];

but would be better to avoid the verification of the storage type, any ideas?

But that approach causes another issue as it will make impossible to use the duplicate feature…