Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSON field 'collMod.validator' is the wrong type 'array', expected type 'object' on Mongo 5 #2396

Closed
douglasjam opened this issue Dec 12, 2021 · 2 comments · Fixed by #2401
Closed
Labels
Milestone

Comments

@douglasjam
Copy link

TL;DR

Running doctrine:mongodb:schema:update with mongo 5 throws a weird error BSON field 'collMod.validator' is the wrong type 'array', expected type 'object' but on Mongo 4.4 works

Detailed version

I have a problem that I'm facing for some days already and have no idea how to fix it, maybe sharing here someone can help me.

When running ./bin/console doctrine:mongodb:schema:update I got a weird Mongo 5 error that I have no idea about it, the error is:

BSON field 'collMod.validator' is the wrong type 'array', expected type 'object'

When doing elimination I excluded document by document in my classes definitions and end-up in the Category class below, I removed the whole document definition leaving only the ID, also tried excluding this class, copying a working one to the same name or different but the same error happens. Tried downgrading many packages but still no success.

When schema:drop the schema:create it works, indexes are there. But the schema:update just crashes.

My last attempt was to downgrade to mongo 4.4, and to my surprise, it works, not sure what happens in version 5 but there is some incompatibility that I can not debug or trace more details.

Just want to share here, maybe someone else faces the same error or has the real solution for the problem.


Environment versions with the error:

mongodb/mongodb: "^1.10",
doctrine/mongodb-odm-bundle: "^4.4",
symfony/*: ^5.4
mongo: 5.0.1
ext-mongo: 1.11.1


All my console output + Logs

console log

/var/www/html # ./bin/console     doctrine:mongodb:mapping:info  
Found 12 documents mapped in document manager default:

[OK]   Project\Api\Category\Document\Category
[OK]   Project\Api\Chat\Document\Chat
[OK]   Project\Api\Chat\Document\Embedded\Message
[OK]   Project\Api\Favorite\Document\Favorite
[OK]   Project\Api\Item\Document\Embedded\Contact
[OK]   Project\Api\Item\Document\Embedded\Price
[OK]   Project\Api\Item\Document\Embedded\Statistics
[OK]   Project\Api\Item\Document\Item
[OK]   Project\Api\Shared\Document\Location
[OK]   Project\Api\Tracking\Document\Tracking
[OK]   Project\Api\User\Document\Embedded\Flag
[OK]   Project\Api\User\Document\User
/var/www/html # ./bin/console  doctrine:mongodb:schema:create
Created collections for all classes
Created indexes for all classes
/var/www/html # ./bin/console doctrine:mongodb:schema:update
Updated indexes for all classes
BSON field 'collMod.validator' is the wrong type 'array', expected type 'object'

mongo.log

[2021-12-12T18:10:20.132428+00:00] doctrine.DEBUG: MongoDB command: {"create":"categories","capped":false,"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:20.170372+00:00] doctrine.DEBUG: MongoDB command: {"create":"chats","capped":false,"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:20.197437+00:00] doctrine.DEBUG: MongoDB command: {"create":"favorites","capped":false,"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:20.225217+00:00] doctrine.DEBUG: MongoDB command: {"create":"items","capped":false,"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:20.252187+00:00] doctrine.DEBUG: MongoDB command: {"create":"tracking","capped":false,"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:20.279191+00:00] doctrine.DEBUG: MongoDB command: {"create":"users","capped":false,"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:21.365906+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"categories","indexes":[{"key":{"url":1},"name":"url","unique":true,"sparse":false,"background":false}],"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:21.403598+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"chats","indexes":[{"key":{"userId":1,"itemId":1},"unique":true,"sparse":false,"background":false,"name":"userId_1_itemId_1"}],"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:21.451128+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"favorites","indexes":[{"key":{"userId":1,"itemId":1},"unique":true,"sparse":false,"background":false,"name":"userId_1_itemId_1"}],"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:21.484963+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"users","indexes":[{"key":{"email":1},"unique":true,"sparse":false,"background":false,"name":"email_1"}],"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:21.503998+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"users","indexes":[{"key":{"username":1},"unique":true,"sparse":false,"background":false,"name":"username_1"}],"$db":"project-test","lsid":{"id":{"$binary":"4JHwx5b+RNW3orE0RYMJcQ==","$type":"04"}}} [] []
[2021-12-12T18:10:27.995243+00:00] doctrine.DEBUG: MongoDB command: {"listIndexes":"categories","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.026341+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"categories","indexes":[{"key":{"url":1},"name":"url","unique":true,"sparse":false,"background":false}],"$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.028161+00:00] doctrine.DEBUG: MongoDB command: {"listIndexes":"chats","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.029907+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"chats","indexes":[{"key":{"userId":1,"itemId":1},"unique":true,"sparse":false,"background":false,"name":"userId_1_itemId_1"}],"$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.031867+00:00] doctrine.DEBUG: MongoDB command: {"listIndexes":"favorites","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.034184+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"favorites","indexes":[{"key":{"userId":1,"itemId":1},"unique":true,"sparse":false,"background":false,"name":"userId_1_itemId_1"}],"$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.037336+00:00] doctrine.DEBUG: MongoDB command: {"listIndexes":"items","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.039223+00:00] doctrine.DEBUG: MongoDB command: {"listIndexes":"tracking","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.041588+00:00] doctrine.DEBUG: MongoDB command: {"listIndexes":"users","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.043923+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"users","indexes":[{"key":{"email":1},"unique":true,"sparse":false,"background":false,"name":"email_1"}],"$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.046046+00:00] doctrine.DEBUG: MongoDB command: {"createIndexes":"users","indexes":[{"key":{"username":1},"unique":true,"sparse":false,"background":false,"name":"username_1"}],"$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.057672+00:00] doctrine.DEBUG: MongoDB command: {"listCollections":1,"nameOnly":false,"$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []
[2021-12-12T18:10:28.064326+00:00] doctrine.DEBUG: MongoDB command: {"collMod":"categories","validator":[],"validationAction":"error","validationLevel":"strict","$db":"project-test","lsid":{"id":{"$binary":"iXJhzTV0QYKdUjgDeBYQgA==","$type":"04"}}} [] []

The Document in which if I remove it works

<?php

declare(strict_types=1);

namespace Project\Api\Category\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Project\Api\Shared\Document\TimeTrait;

/**
 * @ODM\Document(collection="categories", writeConcern="majority")
 * @ODM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 * @ODM\HasLifecycleCallbacks()
 */
class Category
{
    use TimeTrait;
    public const ACTIVE_FIELD = 'active';
    public const URL_FIELD = 'url';

    /** @ODM\Id(strategy="AUTO", name="_id") */
    private ?string $id = null;

    /** @ODM\Field(type="string") */
    private string $name;

    /**
     * @ODM\Field(type="string")
     * @ODM\Index(
     *     name="url",
     *     unique=true,
     *     keys={
     *         "url" = "asc"
     *     }
     * )
     */
    private string $url;

    /** @ODM\Field(type="int") */
    private int $position;

    /** @ODM\Field(type="bool") */
    private bool $active;

    public function getId(): ?string
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getUrl(): string
    {
        return $this->url;
    }

    public function getPosition(): int
    {
        return $this->position;
    }
}

doctrine_mongodb.yaml

doctrine_mongodb:
    auto_generate_proxy_classes: true
    auto_generate_hydrator_classes: true
    connections:
        default:
            server: 'mongodb://%env(MONGODB_HOST)%:%env(MONGODB_PORT)%'

    default_database: '%env(MONGODB_DATABASE)%'
    document_managers:
        default:
            auto_mapping: true
            mappings:
                Project\Api\:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/'
                    prefix: 'Project\Api'
@GuillQuirin
Copy link

GuillQuirin commented Dec 13, 2021

I faced the same error today and it is the $validator variable by default set as an array in ODM/MongoDB/SchemaManager.php in the updateDocumentValidator() method who triggers the error.

If you edit by $validator = (object) []; it works correctly but it is just a quick workaround...

@malarzm
Copy link
Member

malarzm commented Dec 13, 2021

The first step forward would be to add MongoDB 5.0 to the test matrix as we haven't gotten to it yet. Hopefully some of our tests will break :) @douglasjam @GuiReco anybody up for a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants