1

I'm using the mongocxx (v3) driver in my project to store the app settings in the mongodb. While working in windows everything seems to work well. But now that i'm testing in linux i have noticed some problems that i cannot understand why are happening.

i have the following function to retrieve the app settings:

QJsonObject MongoDBManager::getProjectsSettings(QString appid,QString projectsname)
{
    try {

        if(m_selectedDB.has_collection(projectsname.toStdString())==false){

            LOG_ERROR()<<"collection '"+projectsname+"' do not exist";
            return QJsonObject();
        }



        mongocxx::collection selectedCollection = m_selectedDB[projectsname.toStdString()];
        auto cursor =selectedCollection.find(document{}
                                             << "appid"
                                             << appid.toStdString()
                                             << finalize
                                             );

        //    auto cursor = m_selectedCollection.find(document{} << "_id" << appid.toStdString() << finalize);
        for (auto &&doc : cursor) {

            bsoncxx::builder::stream::document s;
             s << "x" << 1.0;

             std::cout<<"testing:" << bsoncxx::to_json(s) << std::endl;

             std::cout<<"doc:"<< bsoncxx::to_json(doc)<<std::endl;

             ....

While in run in debug mode i get the following output:

testing:{ "x" : 1.0 }
doc:{ "_id" : { "$oid" : "5e94da26ef664e37b0d8d1e4" }, "appid" : "5ea8a4192483621670ffeb2b", "value" : 1.1000000000000000888 }

and in release i get:

testing:{ "x" : 1.0 }
doc:{ "_id" : { "$oid" : "5e94da26ef664e37b0d8d1e4" }, "appid" : "5ea8a4192483621670ffeb2b", "value" : 1,1000000000000000888 }

In the database i have:

I have to strange things happening:

  1. In debug mode i have a valid json document

    ... "value" : 1.1000000000000000888 }

    but in release mode i have comma instead of dots in the decimal separator witch obviously is an invalid JSON document.

    ... "value" : 1,1000000000000000888 }

  2. In the DB i have 1.1 why i'm getting 1.1000000000000000888 in my program

Rui Sebastião
  • 855
  • 1
  • 18
  • 36
  • 1
    Comma is used as a decimal separator in many European countries. Do you have your locale set correctly? – Roy2511 May 09 '20 at 13:35
  • yes my local is set to C_CTYPE=pt_PT.UTF-8, but shouldn't the returned value from ::to_json ignore the local and always return dot because that is the standard of json decimal separator, and why debug/release output are not the same? – Rui Sebastião May 09 '20 at 13:49
  • indeed setting LC_NUMERIC="en_US.UTF-8"' solves the problem but its a workaround, don't know if there is other way of solving this – Rui Sebastião May 09 '20 at 17:39
  • I have the exact same problem now. This should be independent of the locale since 1,1000 is not a valid JSON number. Did you find a better way of fixing this? – Callahan Sep 29 '20 at 15:28

0 Answers0