1

I am trying to find all person records that time between start and end and have place id in [1,2] This is my query:

   Person.find({
        time: {
            $gte: start,
            $lt: end
        },
        "place.place_id": { $in: [1,2] }
    }, {
        "time":1,
        "place.$": 1
    },
    function (err, docs) {
        if (err) {
            res.status(500).send(err);
        } else {
            res.status(200).send(docs);
        }
    }
);

But I am getting only one place. This is my output

[
{
    "_id": "5bffc1e9bd35e42020c05cf1",
    "time": "2018-11-29T10:38:01.401Z",
    "places": [
        {
            "_id": "5bffc1e9bd35e42020c05de3",
            "place_id": 1,
            "place_name": "test1"
        }
    ]
},
{
    "_id": "5bffc256bd35e42020c05de4",
    "time": "2018-11-29T10:40:01.324Z",
    "places": [
        {
            "_id": "5bffc256bd35e42020c05ed6",
            "place_id": 1,
            "place_name": "test1",
        }
    ]
}
]

I am getting only place which have id 1 my desired output is

[
{
    "_id": "5bffc1e9bd35e42020c05cf1",
    "time": "2018-11-29T10:38:01.401Z",
    "places": [
        {
            "_id": "5bffc1e9bd35e42020c05de3",
            "place_id": 1,
            "place_name": "test1"
        },
        {
            "_id": "5bffc1e9bd35e42020c05de3",
            "place_id": 2,
            "place_name": "test2"
        }
    ]
},
{
    "_id": "5bffc256bd35e42020c05de4",
    "time": "2018-11-29T10:40:01.324Z",
    "places": [
        {
            "_id": "5bffc256bd35e42020c05ed6",
            "place_id": 1,
            "place_name": "test1",
        },
        {
            "_id": "5bffc256bd35e42020c05ed6",
            "place_id": 2,
            "place_name": "test2",
        }
    ]
}
]

Update Tried this but this gives me empty object

    Persons.find({
        time: {
            $gte: start,
            $lt: end
        },
        places: {
            $filter: {
               input: "$places",
               as: "place",
               cond: { $in: ["$$place.place_id",[ 1,2 ]] }
            }
         }  
    },
    function (err, docs) {
        if (err) {
            res.status(500).send(err);
        } else {
            res.status(200).send(docs);
        }
    }
);

};

What am I doing wrong?? I also tried $elemMatch, $all Operator but same output.

How can I get all these places?

Thanks.

Update2

I am entrying data after every 2 minutes this is start and end values

var end = new Date();
var start = new Date(end);
start.setMinutes(end.getMinutes() - 10);
Ashh
  • 44,693
  • 14
  • 105
  • 132
Unknown
  • 373
  • 1
  • 4
  • 15
  • You need to use `$filter` aggregation to get both the `place_id`. Possible dupe of https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – Ashh Nov 29 '18 at 10:55
  • @Unknown what are the values for `start` & `end`? Could you please update them above? – Schüler Nov 29 '18 at 11:17
  • 1
    @Schüler I have Updated my question – Unknown Nov 29 '18 at 12:23

1 Answers1

1

You need to use aggregation for this. Something like

db.collection.aggregate([
  { "$match": {
    "time": { "$gte": start, "$lt": end },
    "place.place_id": { "$in": [1, 2] }
  }},
  { "$project": {
    "time": 1,
    "places": {
      "$filter": {
        "input": "$places",
        "as": "place",
        "cond": { "$in": ["$$place.place_id", [1, 2]] }
      }
    }
  }}
])
Ashh
  • 44,693
  • 14
  • 105
  • 132