Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

In a simple scenario of me inserting a key-value pair, when value contains some white space, how can I preserve it?

Example:

> > db.bar.insert({"name":"mike    "})
> > db.bar.find() { "_id" : ObjectId("5080c03b8410d443d9acb7f5"), "name" : "mike " }
share|improve this question

1 Answer 1

up vote 2 down vote accepted

A bit of testing shows that the data is being inserted correctly, but the shell is trimming the field when it displays it. You can test this as follows:

mongos> db.test.insert({"name":"    adam    "})
mongos> db.test.find({"name":"    adam    "})
{ "_id" : ObjectId("50814e5c2a17d922cd25974f"), "name" : " adam " }
mongos> db.test.find({"name":" adam "})
mongos>

That is, the data is found when you query with multiple spaces but not found when you re-use what the shell displays. So this is just a display issue with the shell, the DB is behaving as it should behind the scenes. Also tested this with a driver and it behaved correctly.

Even though this means that the data is the way you want, it is still technically a bug, because it could be confusing. So I filed a bug here:

https://jira.mongodb.org/browse/SERVER-7424

Finally, to make this print out correctly, try this (assuming the same data I used in my test):

mongos> var fix = db.test.find({"name":"    adam    "}).next()
mongos> printjsononeline(fix)
{  "_id" : ObjectId("50814e5c2a17d922cd25974f"),  "name" : "    adam    " }
share|improve this answer
    
Thank you Adam! –  Jam Oct 19 '12 at 14:14
    
welcome! I just updated to add that I have filed a bug for the display issue :) –  Adam C Oct 19 '12 at 14:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.