如何调试MongoDB错误"order.addresses.setup is undefined"?

How to debug the MongoDB error "order.addresses.setup is undefined"?

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

我遇到了一个错误。 如果安装地址为空,如何编写查询?

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

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