How to store a period(dot) character appearing in the array key?

480 views
Skip to first unread message

Niranjan

unread,
Nov 11, 2012, 10:52:42 AM11/11/12
to mongod...@googlegroups.com
Hi,

I am trying to '$set' a key-value pair, where the key is a string which has periods in it. Mongodb is not updating such documents.

My document"
{
    "strings" : [ {"this is one string" : 123}, {"another one here" : 432}]
}

I am trying to update strings field, to $set the following the array key-value, where the key has a period:

myArray["This is a sample. Hope it works"] = 946;

When I use $set, the field in the document does not get updated. I am assuming this is due to the presence of the period. 
How do I escape the period for mongo?

Regards,
Niranjan

Rob Moore

unread,
Nov 11, 2012, 12:13:13 PM11/11/12
to mongod...@googlegroups.com

I'm not sure what your desired end state for the document is.  Do you want a document that looks like:

{
    "strings" : [ {"this is one string" : 123}, {"another one here" : 432}],
    myArray[ { "This is a sample. Hope it works" : 946} ]
}

Or are you after:
{
    "strings" : [ {"this is one string" : 123}, {"another one here" : 432}, { "This is a sample. Hope it works" : 946} ],
}

Or something else?

Rob.

Niranjan U

unread,
Nov 11, 2012, 12:25:40 PM11/11/12
to mongod...@googlegroups.com
Hi Rob,

I want the second one:
{
    "strings" : [ {"this is one string" : 123}, {"another one here" : 432}, { "This is a sample. Hope it works" : 946} ], 
}

A little more detail:
I am using PHP MongoDB. This is the update command I use:

$myArray = array("This is a sample. Hope it works" => 946, "this is one string" => 123, "another one here" => 432);

$mongo-> update(array('id' => MongoObjectId), array('$set' => array("strings" => $myArray)));

This update does not work only in the case when an array key exists in myArray with a period character in it. It works fine otherwise. How to escape the period?

Regards.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Rob Moore

unread,
Nov 11, 2012, 1:49:24 PM11/11/12
to mongod...@googlegroups.com

Sorry - Some how I missed that in the first example and the title. 

The answer is you can't escape it.  Field names are not allowed to have a period.

See: https://jira.mongodb.org/browse/SERVER-3229 

Rob.

Derick Rethans

unread,
Nov 11, 2012, 3:12:58 PM11/11/12
to mongod...@googlegroups.com
On Sun, 11 Nov 2012, Niranjan U wrote:

> I want the second one: {
> "strings" : [ {"this is one string" : 123}, {"another one here" : 432},
> { "This is a sample. Hope it works" : 946} ],
> }

I would store that differently. You should try to never have random
arbitrary keys like you have above. Even though MongoDB is schema less,
I would always take as a guidepoint that I always need to be able to
describe what each *key* means. I would instead store the data like
this:

{
"strings" : [
{
string: "this is one string",
value: 123
},
{
string: "another one here",
value: 432
},
{
string: "This is a sample. Hope it works",
value: 946
}
]
}

cheers,
Derick
--
{
website: [ "http://mongodb.org", "http://derickrethans.nl" ],
twitter: [ "@derickr", "@mongodb" ]
}
Reply all
Reply to author
Forward
0 new messages