在尝试查找文档列表时忽略了mongoose中的$ in运算符

时间:2018-06-06 07:11:38

标签: javascript node.js mongodb mongoose

我正在尝试使用$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
}

0 个答案:

没有答案