gpt4 book ai didi

javascript - 在 mongo shell 中使用 printjson 进行 mongoDB 调试

转载 作者:太空宇宙 更新时间:2023-11-04 15:34:30 26 4
gpt4 key购买 nike

我正在尝试在临时编码步骤中使用 printjson 调试 mongoDB 查询。作为中间步骤,我能够从具有不同结构的三个集合中获得类似的 printjson 输出。我认为这允许我从现在开始对所有三个不同的集合使用通用代码。然而,这似乎不起作用。两个集合的行为符合预期,但第三个集合在后续编码步骤中却没有表现(使用 Object.keys 函数)。我有三个问题:

  1. printjson 是一个很好的调试工具吗?
  2. 为什么具有类似 printjson 输出的代码(作为临时步骤)在后续编码步骤中表现不同?
  3. 如何更正 layouts3 中的查询代码以生成与 layouts1layouts2 生成的结果相似的结果?

这三个不同的集合是:layouts1layouts2layouts3:

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") }]
}]}]});

下面是获取三个集合中每个集合的输出所需的代码,该输出似乎具有相似的数据结构(通过查看 printjson 输出来判断)。

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])})})})})

下面的代码适用于 layouts1layouts2,但不适用于 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))}) }) })})})

以下不起作用。它会产生以下错误:TypeError:position[positionKey].forEach不是函数

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))
})})})})})

最佳答案

  1. printjson 似乎是一个很好的调试工具,它帮助我识别了问题
  2. 实际上,代码有一些难以注意到的 printjson 输出。该代码不起作用,在输出顶部有 ObjectId("58e55f0f68afb6085ec3a2cc")
  3. 生成与使用布局1和布局2生成的结果类似的结果所需的代码如下:

    db.layouts3.find().forEach(doc => { doc.pages = doc.pages
    .map(page => { page.positions//page.positions 映射并且是 []
    .forEach(位置 => {Object.keys(位置)
    //过滤需要的“_id”字段,去掉“_id”字段
    .filter(positionKey =>positionKey !== "_id")
    .forEach(positionKey => { 位置[positionKey]
    .forEach( colorDetails => { printjson(Object.keys(colorDetails))
    })})})})})

关于javascript - 在 mongo shell 中使用 printjson 进行 mongoDB 调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44508256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com