您可以使用
.aggregate()方法和
$redact运算符执行此操作。在
$cond表达式中,您需要使用
$setIsSubset来检查“产品”数组中的所有元素是否都在“
productIds”中。这是因为您不能
$in在条件表达式中使用
var productIds = [139,72,73,1,6];db.customers.aggregate([ { "$redact": { "$cond": [ { "$setIsSubset": [ "$products", productIds ] }, "$$KEEP", "$$PRUNE" ] }} ])


