[JAVA-151] Dot allowed in key names Created: 20/Aug/10  Updated: 20/Apr/20  Resolved: 23/Feb/11

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.1
Fix Version/s: 2.5

Type: Bug Priority: Major - P3
Reporter: Tsz Ming Wong Assignee: Antoine Girbal
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-30575 Please add escaping convention for do... Backlog

 Description   

I can save a key with a "dot" using the latest Java client.

This should not be allowed.

import com.mongodb.Mongo;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
 
public class Test {
 
    public static void main(String[] args) throws Exception {
 
        Mongo m = new Mongo();
        DB db = m.getDB("test_db_1");
        DBCollection coll = db.getCollection("test_collection_1");
 
        BasicDBObject doc = new BasicDBObject();
        doc.put("name.first", "First Name");
        doc.put("name.last", "Last Name");
        coll.insert(doc);
 
    }
}



 Comments   
Comment by Antoine Girbal [ 23/Feb/11 ]

fixed and added test

Comment by auto [ 23/Feb/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-151: Dot allowed in key names
https://github.com/mongodb/mongo-java-driver/commit/314a46393748b616ed55b5738cd61d1b72d4ec2f

Comment by Scott Hernandez (Inactive) [ 23/Feb/11 ]

Yep, sounds correct.

Comment by Antoine Girbal [ 23/Feb/11 ]

looking into this, it seems that:

  • for insert/save, the object's keys are checked for "." (anywhere) and "$" (1st char)
  • for all other operations (like update) no check is done.

For example with update I can get:
{ "_id" :

{ "$oid" : "4d645eb2b8fbd47f326e46ad"}

, "a.b" : 31}

My understanding is that driver should apply check on update object, as long as first key does not start with "$".

Comment by Scott Hernandez (Inactive) [ 11/Dec/10 ]

This patch should check for "$" as well.

Comment by auto [ 09/Dec/10 ]

Author:

{u'login': u'bwmcadams', u'name': u'Brendan W. McAdams', u'email': u'brendan@10gen.com'}

Message: JAVA-151: Call _checkObject from Insert() as well as Save() to ensure no keys with . are inserted. Test to expect an exception.
https://github.com/mongodb/mongo-java-driver/commit/b83350c76c41ecbd0da2e482399a8ab8c6cd0aa2

Comment by dustin norlander [ 23/Aug/10 ]

huh, thats very interesting, thanks. I guess I'm very confused.

Comment by Eliot Horowitz (Inactive) [ 23/Aug/10 ]

Not sure what you saw or are expecting...

This behavior has always been there, certainly no changes since 1.4

Comment by dustin norlander [ 23/Aug/10 ]

Ugh, yeah, good point. I guess I have to revert back to 1.4.. Any idea when this one will get fixed?

Comment by Eliot Horowitz (Inactive) [ 23/Aug/10 ]

indexes for example, you often index on "person.state" and so need a key with a '.'

If you try to insert an object with a dot in a field name, it will throw an exception

Comment by dustin norlander [ 23/Aug/10 ]

For us, dot's are never allowed in keys. Are there other places this will break?
What do you mean by "save/insert will throw an exception in the right places" ?

Comment by Eliot Horowitz (Inactive) [ 23/Aug/10 ]

That kind of fix won't work as dotted fields are valid in some places.

save/insert will throw an exception in the right places.

Comment by dustin norlander [ 23/Aug/10 ]

big bug for us. Quick and nasty fix:

Add to BasicDBObject.java

@Override
public Object put(String key, Object val) {
if (key.indexOf(".") != -1)

{ int ind = key.indexOf("."); String k = key.substring(0, ind); String k2 = key.substring(ind+1, key.length()); return this.put(k, new BasicDBObject(k2, val)); }

else

{ return super.put(key, val); }

}

Comment by dustin norlander [ 23/Aug/10 ]

Oh no.. You're right. ack

Comment by Eliot Horowitz (Inactive) [ 23/Aug/10 ]

@dustin - i think you're thinking of something else.

        DBObject o = new BasicDBObject();
        o.put( "a.b" , 1 );
        coll.insert( o );
        
        System.out.println( o );

results in
{ "a.b" : 1 , "_id" : { "$oid" : "4c72a6ed91c273e7ad5577c0"}}

which is definitely a bug

Comment by dustin norlander [ 23/Aug/10 ]

eee gad! this cannot be a bug..

doc.put("name.first", "First Name");

should result in :
{
name :

{ first : "First Name"}

}

This is the expected result, and works fine in 2.1.

PLEASE do not require us to do:

doc.put("name" , new BasicDBObject("first", "First Name"));

I will need to rewrite large portions of my app, and it makes the code much, much, much uglier.

Generated at Thu Feb 08 08:51:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.