MongoException: zero-length keys are not allowed, did you use $ with double quotes?
Asked Answered
S

2

6

I'm using symfony2 and mongodb, until today, everything is OK, but I create a new document, and suddenly, appears this error :

"MongoException: zero-length keys are not allowed, did you use $ with double quotes?"

$dm = $this->get('doctrine.odm.mongodb.document_manager');
$_repo = $dm->getRepository('CantaoCustomerBundle:CustomerTags');
$_repo->findOneByCustomer($customer);

The $customer it's OK, the repository is empty, and my document class is like this :

    /**
     * @MongoDB\ID
     **/
    private $id;

    /** 
     * @MongoDB\ReferenceOne(targetDocument="Tapronto\Mats\ProductBundle\Document\Tag", cascade={"persist"})
     **/
    private $tag;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Tapronto\Mats\CustomerBundle\Document\Customer", cascade={"persist"})
     **/
    private $customer;

    /**
     * @MongoDB\Float
     **/
    private $points;

    /**
     * @MongoDB\Int
     **/
    private $viewed;

    /**
     * @MongoDB\Int
     **/
    private $brought;

    /**
     * @MongoDB\Int
     **/
    private $favorited;

    /**
     * @MongoDB\Date
     * @Gedmo\Timestampable(on="create")
     **/
    private $createdAt;

    /**
     * @MongoDB\Date
     * @Gedmo\Timestampable(on="update")
     **/
    private $updatedAt;

Can anyone help me, have some idea, I tried everything and nothing seems to work

Superpatriot answered 10/6, 2013 at 13:5 Comment(2)
Is it a embedded document?Supernova
No, it's not. In theory, this document make an relationship between customer and tags.. but it's not embedded..Superpatriot
A
5

It could be that you're trying to persist an object private attribute.

If that's not the case a good way to debug is to shut off the zero-length key check so that you can actually debug by checking what it's being written into mongo.

zero-length keys are not allowed, did you use $ with double quotes?

Code: 1

You tried to save "" as a key. You generally should not do this. "" can mess up subobject access and is used by MongoDB internally. However, if you really want, you can set mongo.allow_empty_keys to true in your php.ini file to override this sanity check. If you override this, it is highly recommended that you set error checking to strict to avoid string interpolation errors.

http://php.net/manual/en/mongo.configuration.php#ini.mongo.allow-empty-keys

Appellee answered 12/5, 2015 at 9:39 Comment(0)
B
6

I just fixed this by using the referenced object's ID instead of the reference object itself as my search term.

$_repo->findOneByCustomer($customer->getId());

EDIT: That isn't throwing the exception but it isn't actually returning anything either. I tried using new MongoId($id) as was suggested a few places (Doctrine MongoDB find by id), but that didn't work either. Finally, I found something in the full query builder that searches by references (note: this uses the object instead of the object's ID).

$dm->createQueryBuilder()->find('CantaoCustomerBundle:CustomerTags')
                         ->field('customer')->references($customer)
                         ->getQuery()->execute();

I feel like this should be done more simply (like you did originally), but this fix is working for me.

Bistre answered 12/9, 2014 at 2:26 Comment(0)
A
5

It could be that you're trying to persist an object private attribute.

If that's not the case a good way to debug is to shut off the zero-length key check so that you can actually debug by checking what it's being written into mongo.

zero-length keys are not allowed, did you use $ with double quotes?

Code: 1

You tried to save "" as a key. You generally should not do this. "" can mess up subobject access and is used by MongoDB internally. However, if you really want, you can set mongo.allow_empty_keys to true in your php.ini file to override this sanity check. If you override this, it is highly recommended that you set error checking to strict to avoid string interpolation errors.

http://php.net/manual/en/mongo.configuration.php#ini.mongo.allow-empty-keys

Appellee answered 12/5, 2015 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.