Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle document from mongoDB with _bsontype properties

Hi I'm walking through a JSON array resulting of a query of documents in mongoDB. The fact is that I'm getting the following behaviour and I'm don’t know why I'm getting this:

in key: _bsontype |value: ObjectID
in key: id |value: S÷¯çò9þ w
in key: _bsontype |value: ObjectID
in key: id |value: S÷¯çò9þ h
in key: _bsontype |value: ObjectID
in key: id |value: S÷¯çò9þ h
in key: name |value: Default Process
in key: processType |value: Public
in key: id |value: BPMNDiagram_1
in key: name |value: procurement subprocess

As you can see, this is wear... this is my code:

function changeIDs(json, map, count){
  for(var key in json){
    if(json.hasOwnProperty(key))
    if(typeof json[key] === 'object')
      changeIDs(json[key], map, count);
    else{
        console.log("in key: "+key + " |value: "+json[key]);
    }

  }
}

And this is a part of my input ( json parameter ):

[
    {
        "_id": "538df78eafe7f28d39fe2077",
        "processId": "538df71bafe7f28d39fe2068",
        "processMeta": {
            "id": "538df71bafe7f28d39fe2068",
            "name": "Default Process",
            "processType": "Public"
        },
        "diagram": {
            "id": "BPMNDiagram_1",
            "name": "procurement subprocess"
        },
        "plane": {
            "id": "BPMNPlane_1",
            "bpmnElement": "538df71bafe7f28d39fe2068"
        }
    },
{other objects..},{other objects..}
]

Yes, processId, and _id are generated with the ObjectId function, and seems here where the problem appears, I'm not sure about this, but my guess is that each _bsontype correspond to mongoSB Object Id and this is an object so my function go inside this recursively.. getting the following S÷¯çò9þ w..,Am I right ?

Also seems like if I'm not able to get the "processId" and "_id" keys in my for, because of that, I guess are my _bsontype... So at the end, my question is, How can I walk through my object without getting that results ?, you have to see there is an "id" property that contains crap data and I don’t want it in my result when looking for all keys id for example, BUT still be able to get the ObjectId.str value for my properties "processId" or "_id".

Thanks in advance.

like image 310
rahpuser Avatar asked Jan 10 '23 11:01

rahpuser


1 Answers

Just in case, if in the future some other unlucky guy as me get the same problem.

As you can see my function changeIDs get 3 parameters, I just change the json parameter, it was supposed to be a json ( and it was ) you can see my json output as text in my question, so it is a valid json, however I did a parse of a stringify string.. and after that I try it again.. and it was working.

So the solution? newJson = JSON.parse(JSON.stringify(json));

Why? well my better bet is that in this way, when we do a stringify of an object, it object change in the following way:

  • Undefined values disappear in the result.
  • Lose prototypes properties
  • The functions die

For example if a json have an object DATE this object would be stringify as Date.toString(), so it would storage the string representation. and if the function does not have a to string would be undefined.

This last is why after doing an stringify it is working, My function ObjectId() didn't survive to my stringify, but it transform to the string value it contains, that is the Mongo object id value...

like image 113
rahpuser Avatar answered Jan 22 '23 01:01

rahpuser