Why does mongo crop space while saving?

I notice that MongoDB collapses multiple spaces together (in the same space) while saving documents in a collection. These are not just leading and trailing spaces, but any sequence of spaces - and only spaces, and not all spaces (checked only by a space and a tab). This happens when using the Java driver, as well as the mongo interactive shell, so I assume this is a โ€œfunctionโ€ of the data warehouse itself.

Admittedly, I do not like extraneous gaps in my meanings, and I discovered this in the process of eliminating them. However, this behavior seems strange, because, as a rule, data storages, as a rule, refrain from "preparing" or otherwise changing data that exceeds the minimum necessary to meet basic storage restrictions. Doing otherwise (where not announced to users / developers) may result in loss of data or accuracy. Also, why is the collapse of space instead of completely trimming them along the head and tail - and why do they destroy them between non-spatial characters?

> db.test.remove() > db.test.save({x: " x \t\t\tx "}) > db.test.findOne() { "x" : " x \t\t\tx " } 

Am I mistakenly turning this feature on or is it turned on by default? I could not find anything on JIRA . This seems like a mistake to me, but maybe I'm just special. MongoDB Version 2.0.2

+6
source share
1 answer

Works on 2.0.X and 2.1.X:

 > db.version() 2.1.0-pre- > db.test.remove() > db.test.save({x: " x \t\t\tx "}) > db.test.findOne() { "_id" : ObjectId("4f3249e80b74284ac62e629d"), "x" : " x \t\t\tx " } > 
+2
source

Source: https://habr.com/ru/post/907895/


All Articles