我正在尝试使用$in
运算符查找文档列表,仅在今天使用todays数据传递到服务器数组时获取数据,但该方法只是忽略它并返回文档列表使用我附加的其余参数而没有$in
那个应该仅在今天过滤数据的参数。
前面:
fetchTodaysCoordsFromServer = () => {
let today = new Date().toString().split(' ');
today = today.slice(0, 4);
this.clearSumAll();
let placeHolderForData = [];
axios
.post('https://server.com/fetchtodayscoords', {
type: this.state.type,
userName: this.props.userName,
today: today
})
.then((res) => {
res.data.map((details) => {
placeHolderForData.push(details.sumAll)
})
this.setState({
sumAll:placeHolderForData
})
})
.catch(() => {
console.log('there was problem fetching todays coords')
})
}
回复:
router.post('/', (req, res) => {
if (req.body.type === 'user') {
MapData.find(
{
date: { $in : req.body.today},
show: true,
userName: req.body.userName
}, // find all documents that contains today date
(err, data) => {
if (err) {
console.log('couldnt fetch coords: ' + err);
res.send(err);
}
if (data) {
console.log(`this is data from user ${data}`);
res.send(data);
}
}
);
}
})
模特:
let MapData = mongoose.model('mapData', {
userName: {
type: String
},
date: {
type: Array
},
show: {
type: Boolean
}
});
文件示例:
{
"_id": {
"$oid": "5b1673c8ba283d0014750346"
},
"date": [
"Tue",
"Jun",
"05",
"2018"
],
"userName": "user1",
"show": true,
"__v": 0
}