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", []]}}