0
我正在使用猫鼬来连接和查询mongoDB。来自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变量来解决这个问题。
如果我将它作为“start-offer-service-dev”在npm脚本中传递:“SAMPLE_SIZE = 2 node_modules/.bin/babel-node src/services /offerService/index.js“,那么它会是一个字符串还是数字? –
是的,我的错误,通过npm脚本传递的参数是字符串类型,因此必须转换为int –
查看我的更新:)很高兴你知道了。干杯。 – matt