每当我删除带有仅匹配的样本的行时,代码工作正常,但是当我像这样添加它时:
Model.aggregate(
{$match: {}},
{$sample: {size: 2}},
(err, rsp)=>{
callback(err,rsp);
});
它会抛出一个错误,说第一个参数必须是字符串或缓冲区。它只能由{$ sample:{size:2}}引起。为什么?样品不能用于猫鼬吗?
试试这个 :
model.instance.aggregate([{ $match: buildQuery(model) }]).sample(20).exec(function (err, results) {
console.log(model.size, results.length);
});