1

I am trying to find elements from my MongoDB database with meteor. I managed to filter and go through the structure of my array, but the result is a single element, and not all the elements matching the criteria.

Query :

var json = Tests1VerlIR.find({}, {fields: {entries: {$elemMatch: {'payload.id': {$eq: this.params._id}} } } }).fetch();
this.response.setHeader('Content-Type', 'application/json');
this.response.end(JSON.stringify(json));

Data Structure :

{"entries":
   [{"method":"POST",
   "source":"ex",
   "path":"/ex",
   "time":1464615406900,
   "payload":        
        {"slot_frame_number":"4",
         "slot_HTUTemp":"2306",
         "data":"0400f008561655270209a314",
         "slot_BMEPres":"10069",
         "slot_HTUHumi":"5283",
         "slot_BMETemp":"2288",
         "time":"1464615404",
         "device":"79",
         "slot_BMEHumi":"5718",
         "signal":"7.22",
         "id":"2"},
   "_id":"574c41ee578d01af3664cbaf"},
   {"method":"POST",
   "source":"ex",
   "path":"/ex",
   "time":1464615406900,
   "payload":        
        {"slot_frame_number":"4",
         "slot_HTUTemp":"2306",
         "data":"0400f008561655270209a314",
         "slot_BMEPres":"10069",
         "slot_HTUHumi":"5283",
         "slot_BMETemp":"2288",
         "time":"1464615404",
         "device":"79",
         "slot_BMEHumi":"5718",
         "signal":"7.22",
         "id":"2"},
   "_id":"574c41ee578d01af3664cbaf"}, {...}]}

Response :

[
    {
        "_id":
        {
            "_str": "576155d7a605348159cd1f1a"
        },
        "entries":
        [
            {
                "method": "POST",
                "source": "ex",
                "path": "/ex",
                "time": 1464615406900,
                "payload":
                {
                     "slot_frame_number":"4",
                     "slot_HTUTemp":"2306",
                     "data":"0400f008561655270209a314",
                     "slot_BMEPres":"10069",
                     "slot_HTUHumi":"5283",
                     "slot_BMETemp":"2288",
                     "time":"1464615404",
                     "device":"79",
                     "slot_BMEHumi":"5718",
                     "signal":"7.22",
                     "id":"2"
                },
                "_id": "574c41ee578d01af3664cbaf"
            }
        ]
    }
]
Adrien Chapelet
  • 386
  • 4
  • 24

2 Answers2

1

You cannot return multiple elements of an array matching your criteria in any form of a basic .find() query. To match more than one element you need to use the .aggregate() method instead.

refer this link.

Tests1VerlIR.aggregate([
{ "$match": { "entries.payload.id": "2" } },

    // Unwind the array to denormalize
    { "$unwind": "$entries" },

    // Match specific array elements
    { "$match": { "entries.payload.id": "2" } },

    // Group back to array form
    { "$group": {
        "_id": "$_id",
        "entries": { "$push": "$entries" }
    }}
])
Community
  • 1
  • 1
vartika
  • 494
  • 2
  • 12
-1

Solution :

var json = Tests1VerlIR.aggregate({"$unwind": "$entries"}, {$match: {'entries.payload.id': this.params._id} });
Adrien Chapelet
  • 386
  • 4
  • 24