0

I have a get router that gets orders with the time stamp

router.get('/api/order/:date/:time', function (req,res,next) {
  Box.find({orders: {date:req.params.date}}.then (function (order){
       console.log('GET ORDER / with ', req.params.date);
       res.send(order);
   }).catch(next);
});

the :time is just to allow my frontend to call this specific get which offers a timestamp inside the :date parameter

now, the model

const orderSchema = new Schema({
    name : { type : String },
    date : { type : String }, // date stamp of only YYYY/MM/DD
    orders : { type : Array}
});

Inside this array of orders you can find elements such as:

"orders" : [ 
        {
        "type" : "Meat Lover Slice",
        "extraType" : "na",
        "extraInfo" : "na",
        "date" : "2018-09-27:08:47:07",
        "quantity" : "1",
        "size" : "medium",
        "crust" : "normal",
        "split" : false,

and so on.. (15 or so elements)

You can see inside this array of orders, there are time stamped with YYYY/MM/DD:HH:MM:SS (2018-09-27:08:47:07).

inside the router, I do get

console.log('GET ORDER / with ', req.params.date) // > GET ORDER / with 2018-09-27:08:47:07

so it does receive the time at this route.

But with the params, how do I filter out the the specific orders matching that element?

order object

Robolisk
  • 1,682
  • 5
  • 22
  • 49
  • Could you try and provide an example of what object types are inside your "inside" array? I'd also consider renaming some portions of your abstract example to be a bit more concrete as well, it would make it a lot easier to understand. – Elliot Blackburn Sep 27 '18 at 14:13
  • @ElliotBlackburn I've updated my question. Hopefully this is sufficient information. – Robolisk Sep 27 '18 at 15:08

1 Answers1

0

If I have understood the question correctly is the short answer is that you can't, there is no way to "filter" sub-documents in a standard query. (Have a look at Return only matched sub-document elements within a nested array for a more in depth answer.)

What you could do is either use MongoDB Aggregations (https://docs.mongodb.com/manual/aggregation/) or do the filtering yourself when you have received the query result. I would suggest doing the filtering yourself.

Petter
  • 1,327
  • 11
  • 12