趟过mongo的坑 第一弹

1.非原创,是遇到问题后google出来的。

db.users.aggregate([
  {$lookup: {
    from: "orders",

    localField: "_id",

    foreignField: "leased_by",

    as: "orders"

 }},

{
   $project: {

    orders: {$size: "$orders"}

  }
}

])

上述写法报错

MongoError: The argument to $size must be an Array, but was of type: EOO.


解决方案:

orders: {$size: { "$ifNull": ["$orders", []]}}

你可能感兴趣的:(趟过mongo的坑 第一弹)