0

I am trying to find a list of documents using the $in operator to fetch data only for todays date when I am passing to the server array with todays data, but the method just ignore it and return list of documents using the rest of the parameters I attached without the $in one that supposed to filter data only for today.

Front:

  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')
      })
  }

BACK:

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);
                }
            }
        );
    }

})

MODEL:

let MapData = mongoose.model('mapData', {
    userName: {
        type: String
    },
    date: {
        type: Array
    },
    show: {
        type: Boolean
    }

});

EXAMPLE OF DOCUMENT:

{
    "_id": {
        "$oid": "5b1673c8ba283d0014750346"
    },
    "date": [
        "Tue",
        "Jun",
        "05",
        "2018"
    ],
    "userName": "user1",
    "show": true,
    "__v": 0
}
obiwankenoobi
  • 1,504
  • 5
  • 18
  • 37
  • What is `req.body.today`? Show the value being sent. Also you appear to have the function backwards. `$in` is for a "list of arguments" and not just for "matching against a list". It's a common mistake as `"date": "05"` as the query condition works just fine and MongoDB does not care if the value is in an array. Nonetheless, this is a really bad idea. Just store a BSON Date as you don't seem to realize how much space you are wasting doing this. Very easy to match "one day" with a BSON Date. – Neil Lunn Jun 06 '18 at 07:16
  • console.log(req.body.today) // Tue,Jun,05,2018 – obiwankenoobi Jun 06 '18 at 07:19
  • Figured as much. It just clicked to me after writing what you were attempting to do. That's just the wrong approach. Store the BSON Date and find the date by `"date": { "$gte": new Date("2018-06-05"), "$lt": new Date("2018-06-06") }`. That's how you find "a day". And not by storing parts in an array and using very inefficient queries to match all array elements. Which as pointed in one of the duplicates was not an `$in` anyway. Store BSON Dates like the rest of the world does. – Neil Lunn Jun 06 '18 at 07:22
  • Thanks for the tip! will do so (/ – obiwankenoobi Jun 06 '18 at 07:25

0 Answers0