Home > Software engineering >  Problem with specific fields while querying mongoDB
Problem with specific fields while querying mongoDB

Time:07-05

The problem is with these two fields : Relative Time(h:min:s.ms) and Real time(h:min:s.ms)

The values cannot be retrieved While trying to export the collection to csv/json. So I tried the following:

  1. full query, able to get the values for both fields.
> db.cell19L28A.find()
{ "_id" : ObjectId("62be726ae6168c39e037bdd5"), "State" : "Rest", "Jump" : 1, "Cycle" : 1, "Steps" : 1, "Current(A)" : 0, "Voltage(V)" : 3.3965, "Capacity(Ah)" : 0, "Energy(Wh)" : 0, "Relative Time(h:min:s.ms)" : "0:00:00.000", "Real time(h:min:s.ms)" : "2022-07-01 09:30:46", "Process Name" : "Charge.xml", "Start Time" : "2022-07-01 09:30:46" }
  1. able to get the values with _id = 0
> db.cell19L28A.find({},{"_id":0})
{ "State" : "Rest", "Jump" : 1, "Cycle" : 1, "Steps" : 1, "Current(A)" : 0, "Voltage(V)" : 3.3965, "Capacity(Ah)" : 0, "Energy(Wh)" : 0, "Relative Time(h:min:s.ms)" : "0:00:00.000", "Real time(h:min:s.ms)" : "2022-07-01 09:30:46", "Process Name" : "Charge.xml", "Start Time" : "2022-07-01 09:30:46" }
  1. But when tried with 1 for both of those fields, an empty string is returned.
> db.cell19L28A.find({},{"_id":0,"Relative Time(h:min:s.ms)":1,"Real time(h:min:s.ms)":1})
{  }
{  }
{  }

  1. able to get the values when given 0 for all the rest of the fields.
> db.cell19L28A.find({},{ "_id" : 0, "State" : 0, "Jump" : 0, "Cycle" : 0, "Steps" : 0, "Current(A)" : 0, "Voltage(V)" : 0, "Capacity(Ah)" : 0, "Energy(Wh)" : 0,  "Process Name" : 0, "Start Time" :0 })
{ "Relative Time(h:min:s.ms)" : "0:00:00.000", "Real time(h:min:s.ms)" : "2022-07-01 09:30:46" }

Can someone please help me understand what is going on here!

CodePudding user response:

Apparently, the issue is with dot (.) in key name Relative Time(h:min:s.ms), Real time(h:min:s.ms),

There are so many restrictions and limitations if the key name contains dot (.), some are resolved in the latest MongoDB v5,

In your case, the projection considers it as an embedded object like this,

{ 
  "Relative Time(h:min:s": { 
    "ms)": 1 
  } 
}

because it contains a dot (.) in the key name, so it can't find the field and it doesn't exist.

You can use $getField from MongoDB v5.0, but it will return an object instead of your original key name,

db.cell19L28A.find({},
{
  "_id": 0,
  "Relative Time(h:min:s.ms)": {
    $getField: "Relative Time(h:min:s.ms)"
  },
  "Real time(h:min:s.ms)": {
    $getField: "Real time(h:min:s.ms)"
  }
})

Result:

[
  {
    "Real time(h:min:s": {
      "ms)": "2022-07-01 09:30:46"
    },
    "Relative Time(h:min:s": {
      "ms)": "0:00:00.000"
    }
  }
]

Playground

I don't see any valid solution for this, my suggestion is to avoid the dot (.) and dollar ($) in the key name.

The values cannot be retrieved While trying to export the collection to csv/json.

There is a warning provided in MongoDB documentation,

Import and Export Concerns With Dollar Signs ($) and Periods (.)

Starting in MongoDB 5.0, document field names can be dollar ($) prefixed and can contain periods (.). However, mongoimport and mongoexport may not work as expected in some situations with field names that make use of these characters.

  • Related