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

Field names with dots can be inserted if the field is > 1 level deep in array #193

Closed
Zelfapp opened this issue Jun 8, 2016 · 5 comments
Labels

Comments

@Zelfapp
Copy link

Zelfapp commented Jun 8, 2016

Not sure if the library is at fault or ext-mongodb 1.1.7, but field names with dots should generate a MongoException regardless of depth of array in which they are found, since mongo 3.x no longer allows dots in field names.

This issue was identified when we attempted to incorrectly insert documents with dots in field names.

For example, this will insert into mongo 3.x:

$data = [
    "abc" => [
        "Leather.CPO.Nametag" => 1   
    ]
];

$collection->insertOne($data);

{
    "_id" : ObjectId("57589f59ae8c50f34f8b4567"),
    "abc" : {
        "Leather.CPO.Nametag" : NumberLong(1)
    }
}

If the array is 1 level, then insert like below will fail properly:

$data = [
     "Leather.CPO.Nametag" => 1
];

$collection->insertOne($data);

PHP Fatal error:  Uncaught exception 'MongoException' with message ''.' not allowed in key: Leather.CPO.Nametag' in -:12

Attempting shell insert when > 1 level deep does fail correctly though:

db.ezrshop_builders.insert(
... {
...     "abc" :
...         {
...             "Leather.CPO.Nametag":1
...         }
... }
... )
2016-06-08T15:46:02.984-0700 E QUERY    Error: can't have . in field names [Leather.CPO.Nametag]
@Zelfapp Zelfapp changed the title Allows fields with dots if the field is > 1 level deep Allows field names with dots if the field is > 1 level deep Jun 8, 2016
@Zelfapp Zelfapp changed the title Allows field names with dots if the field is > 1 level deep Field names with dots can be inserted if the field is > 1 level deep in array Jun 8, 2016
@lindelius
Copy link

lindelius commented Jun 9, 2016

I actually don't get an exception no matter what nesting level I try with. This is clearly a bug (missing constraint) in the driver as this shouldn't be permitted, and isn't if you try to insert it directly via the shell.

@jmikola jmikola added the bug label Jun 14, 2016
@jmikola
Copy link
Member

jmikola commented Jun 14, 2016

PHP Fatal error:  Uncaught exception 'MongoException' with message ''.' not allowed in key: Leather.CPO.Nametag' in -:12

MongoException is not part of this library or ext-mongodb. It was only a class in ext-mongo (i.e. the legacy extension). Can you re-check your example?

That said, it appears the current extension doesn't check for dots in field names at all:

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['foo.bar' => 1]);
$bulk->insert(['x' => ['foo.bar' => 1]]);

$manager = new MongoDB\Driver\Manager(URI);
$manager->executeBulkWrite('test.foo', $bulk);

If I had to guess, we wrongfully assumed that the libmongoc API we use for handling writes was doing this already. I've opened PHPC-712 to track this.

@7snovic
Copy link

7snovic commented May 1, 2017

this is pretty old issue, but now mongo does not throw any errors and accepts the insertion even if it contains periods in the first level.

Mongo version : 2.6.10
PHP version: 7.0.15

@jmikola
Copy link
Member

jmikola commented May 1, 2017

Driver-side validation of insert documents is addressed by PHPC-712 and will be released in ext-mongodb 1.3.0. Note that the validation has nothing to do with the library (this repository).

but now mongo does not throw any errors and accepts the insertion even if it contains periods in the first level.

This issue is referring to newer server versions. Version 2.6 was end-of-lifed in October 2016.

@jmikola
Copy link
Member

jmikola commented Feb 27, 2018

Closing this, as PHPC-712 was fixed in ext-mongodb 1.3.0.

@jmikola jmikola closed this as completed Feb 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants