【发布时间】:2017-02-14 13:50:49
【问题描述】:
我正在使用 mongoose 连接和查询 mongoDB。
现在如果我有以下代码:
return new Promise((resolve) => {
mongoose.connection.db.collection('offer').aggregate([{
$match: {
$or: [
{
ccChecked: {
$lt: new Date(currentDay + 'T00:00:00.000Z')
}
},
{
ccChecked: {
$exists: 0
}
}
],
_id: { $in: offerObjIds }
}
},
{ $sample: { size: 2 } }
], (err, res) => {
console.log('res is', res);
resolve(res);
});
});
提供的结果很好,我们得到了预期的输出。 但是如果我们有以下查询,并且我提供 SAMPLE_SIZE 为 2:
const sampleSize = process.env.SAMPLE_SIZE
return new Promise((resolve) => {
mongoose.connection.db.collection('offer').aggregate([{
$match: {
$or: [
{
ccChecked: {
$lt: new Date(currentDay + 'T00:00:00.000Z')
}
},
{
ccChecked: {
$exists: 0
}
}
],
_id: { $in: offerObjIds }
}
},
{ $sample: { size: sampleSize } }
], (err, res) => {
console.log('res is', res);
resolve(res);
});
});
在这种情况下,我得到的结果是未定义的。有人可以解释为什么会出现这种行为以及如何通过 process.env 变量解决这个问题。
【问题讨论】:
标签: node.js mongodb mongoose mongoose-schema