Ragy Isaac
Ragy Isaac

Reputation: 1458

mongoDB debugging with printjson in mongo shell

I am trying to debug mongoDB query using printjson in interim coding steps. I was able to achieve similar printjson output from three collections with different structures as an intermediate step. I thought this allows me to use common code, from this point on, for all the three different collections. However, this did not appear to work. Two collections behaved as expect but the third did not in the subsequent coding step (using Object.keys function). I have three questions:

  1. Is printjson a good tool for debugging?
  2. Why does code with similar printjson output, as an interim step, behave differently in the subsequent coding step?
  3. How can I correct the query code in layouts3 to produce similar result to that produced with layouts1 and layouts2?

The three different collections are: layouts1, layouts2, and layouts3:

db.layouts1.insert({
    "_id": ObjectId("58e574a768afb6085ec3a388"),
    "positions": [{
            "_id":      ObjectId("58e55f0f68afb6085ec3a2cc"),
            "cyan" :    [{"unit": "08","side": "5","position": "Far","_id": ObjectId("58e55f0f68afb6085ec3a2d0")}],
            "magenta":  [{"unit": "08","side": "5","position": "Far","_id": ObjectId("58e55f0f68afb6085ec3a2cd")}],
            "yellow":   [{"unit": "08","side": "3","_id": ObjectId("58e55f0f68afb6085ec3a2ce")}],
            "black":    [{"unit": "08","side": "5","position": "Far","_id": ObjectId("58e55f0f68afb6085ec3a2cf")}]
        }]
});

db.layouts2.insert({
    "_id" : ObjectId("58e574a768afb6085ec3a388"),
    "pages" : [{
            "_id"       : ObjectId("58e542fb68afb6085ec3a1d2"),
            "positions" : [ 
                        {"cyan":     [{ "unit" : "08", "side" : "5","position" : "Far","_id" : ObjectId("58e542fb68afb6085ec3a1d6")}]}, 
                        {"magenta":  [{"unit" : "08","side" : "5","position" : "Drive Side Far","_id" : ObjectId("58e542fb68afb6085ec3a1d3")}]},
                        {"yellow":   [{"unit" : "08","side" : "3","position" : "Far","_id" : ObjectId("58e542fb68afb6085ec3a1d4")}]},
                        {"black":    [{"unit" : "08","side" : "5","position" : "Far","_id" : ObjectId("58e542fb68afb6085ec3a1d5")}]}
                            ]
                        }]
            });

db.layouts3.insert({
    "_id":              ObjectId("58e574a768afb6085ec3a388"),
    "pages": [{
        "_id":          ObjectId("58e542fb68afb6085ec3a1d2"),
        "positions":    [{ "_id": ObjectId("58e55f0f68afb6085ec3a2cc"),
            "cyan":     [{ "unit": "08", "side": "5", "position": "Far", "_id": ObjectId("58e55f0f68afb6085ec3a2d0") }],
            "magenta":  [{ "unit": "08", "side": "5", "position": "Far", "_id": ObjectId("58e55f0f68afb6085ec3a2cd") }],
            "yellow":   [{ "unit": "08", "side": "3", "_id": ObjectId("58e55f0f68afb6085ec3a2ce") }],
            "black":    [{ "unit": "08", "side": "5", "position": "Far", "_id": ObjectId("58e55f0f68afb6085ec3a2cf") }]
        }]}]});

Below is the code needed to get an output that appears to have similar data structure, as judged by viewing printjson output, for each of the three collections.

db.layouts1.find().forEach(doc => { doc.positions = doc.positions
    .map(position => { Object.keys(position) // Returns a new array conaining all keys for positions
        .filter(positionKey => positionKey !== "_id") // Returns a new array without _id
        .forEach(positionKey => { printjson(position[positionKey])})})})

db.layouts2.find().forEach(doc => { doc.pages = doc.pages
.map( page => { page.positions
    .forEach( position => { Object.keys(position)
        .forEach( positionKey => {printjson(position[positionKey])})})})})


db.layouts3.find().forEach(doc => { doc.pages = doc.pages
    .map(page => { page.positions // page.positions mapped and is []
        .forEach(position => { Object.keys(position) // returns one array with _id, cyan, magenta, etc.
            .forEach(positionKey => { printjson(position[positionKey])})})})})

Below is the code that works for layouts1, layouts2, but does NOT for layouts3:

db.layouts1.find().forEach(doc => { doc.positions = doc.positions
    .map(position => { Object.keys(position) // Returns a new array conaining all keys for positions
        .filter(positionKey => positionKey !== "_id") // Returns a new array without _id
        .forEach(positionKey => { position[positionKey]
            .forEach(colorDetails => { printjson(Object.keys(colorDetails))
            })})})})

db.layouts2.find().forEach(doc => { doc.pages = doc.pages
.map( page => { page.positions
    .forEach( position => { Object.keys(position)
        .forEach( positionKey => {position[positionKey]
        .forEach( colorDetails => { printjson(Object.keys(colorDetails))}) }) })})})

THE FOLLOWING DOES NOT WORK. It produces the following error: TypeError: position[positionKey].forEach is not a function

db.layouts3.find().forEach(doc => { doc.pages = doc.pages
    .map(page => { page.positions // page.positions mapped and is []
        .forEach(position => {Object.keys(position)
            .forEach(positionKey => { position[positionKey]
                .forEach( colorDetails => { printjson(Object.keys(colorDetails))
                })})})})})

Upvotes: 1

Views: 779

Answers (1)

Ragy Isaac
Ragy Isaac

Reputation: 1458

  1. printjson appears to be a good tool for debugging, It helped me identify the problem
  2. Actually the code has slightly, and difficult to notice, printjson output. The code, which did not work, had ObjectId("58e55f0f68afb6085ec3a2cc") on top of the output
  3. The code needed to produce similar result to that produced with layouts1 and layouts2 is as follows:

    db.layouts3.find().forEach(doc => { doc.pages = doc.pages .map(page => { page.positions // page.positions mapped and is [] .forEach(position => {Object.keys(position) // Filter the "_id" field which is needed to remove the "_id" field .filter(positionKey => positionKey !== "_id") .forEach(positionKey => { position[positionKey] .forEach( colorDetails => { printjson(Object.keys(colorDetails)) })})})})})

Upvotes: 1

Related Questions