【问题标题】:How to debug the MongoDB error "order.addresses.setup is undefined"?如何调试 MongoDB 错误“order.addresses.setup is undefined”?
【发布时间】:2018-05-10 15:20:27
【问题描述】:
db.getCollection('order').find({bundleProducts:{$elemMatch:{id:"5720a0ce7709"}}}).forEach(
function(order){ print(
    "invoice addresses: " + order.addresses.invoice.city + "," +
    " delivery addresses: "+ order.addresses.delivery.city+","+
    " setup addresses: "+ order.addresses.setup.city+","); }) ;

当我运行上述问题时

执行脚本失败。

Error:
TypeError: order.addresses.setup is undefined :
@(shell):5:1
DBQuery.prototype.forEach@src/mongo/shell/query.js:501:1
@(shell):1:1

我遇到了一个错误。 安装地址为空怎么写查询?

【问题讨论】:

    标签: javascript mongodb shell mongodb-query robo3t


    【解决方案1】:

    您似乎正在尝试访问不存在的字段 (setup),这就是您收到错误的原因,如果您想排除不包含 setup 的文档,您可能需要使用 $exists 运算符字段,这里是一个例子:

    db.getCollection('order').find({
        bundleProducts: {
            $elemMatch: { id: "5720a0ce7709" }
        },
        "addresses.setup": { $exists: true }
    }).forEach( function(order){ print(
        "invoice addresses: " + order.addresses.invoice.city + "," +
        " delivery addresses: " + order.addresses.delivery.city + ","+
        " setup addresses: " + order.addresses.setup.city + ","); 
    });
    

    【讨论】:

    • 非常感谢 :)),我正在试验 if else 运算符。 $exist 运算符更好.. 谢谢:)
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多