0

i have below collection named Results.

_id                 "9FK5k755ueAYdfip3"
    createdAt       Date {Sat Mar 12 2016 19:58:46 GMT+0100 (CET)}
    results         [Object { errorId="uX6byeiuGjRNXTj6s",  error="02/09/15 13:01:29  backu...ox file was not found. ",  time="02/09/15 13:01:29"}, 
                    Object { errorId="uX6byeiuGjRNXTj6s",  error="02/09/15 13:01:29  backu...ox file was not found. ",  time="02/09/15 13:01:29"},
                    Object { errorId="uX6byeiuGjRNXTj6s",  error="02/09/15 13:22:42  backu...ox file was not found. ",  time="02/09/15 13:22:42"}, 
                    432 more...]

    results:

    0   Object { errorId="uX6byeiuGjRNXTj6s",  error="02/09/15 13:01:29  backu...ox file was not found. ",  time="02/09/15 13:01:29"}  
    1   Object { errorId="uX6byeiuGjRNXTj6s",  error="02/09/15 13:01:29  backu...ox file was not found. ",  time="02/09/15 13:01:29"}   
    2   Object { errorId="uX6byeiuGjRNXTj6s",  error="02/09/15 13:22:42  backu...ox file was not found. ",  time="02/09/15 13:22:42"}

    ...

    14    Object { errorId="5vfLjdbaQLgbuCiNZ",  error="02/09/15 16:04:10  backu...minated with an error. ",  time="02/09/15 16:04:10"}   
    15    Object { errorId="5vfLjdbaQLgbuCiNZ",  error="02/09/15 16:04:10  backu...minated with an error. ",  time="02/09/15 16:04:10"}   
    16    Object { errorId="5vfLjdbaQLgbuCiNZ",  error="02/09/15 16:04:10  backu...minated with an error. ",  time="02/09/15 16:04:10"}

    ...

    183    Object { errorId="uX6byeiuGjRNXTj6s",  error="03/13/15 13:36:24  backu...ox file was not found. ",  time="03/13/15 13:36:24"}
    184    Object { errorId="uX6byeiuGjRNXTj6s",  error="03/13/15 13:42:39  backu...ox file was not found. ",  time="03/13/15 13:42:39"} 
    185    Object { errorId="uX6byeiuGjRNXTj6s",  error="03/13/15 13:42:40  backu...ox file was not found. ",  time="03/13/15 13:42:40"}

I need query which will return all documents for specific errorId in arrays results.

I have tried different queries but nothing worked.

For example:

    Results.find ({ results: { $elemMatch : { errorId: 'uX6byeiuGjRNXTj6s'}}})
    Results.find ({ results: { $elemMatch : { Key: 'errorId', 'errorId': 'uX6byeiuGjRNXTj6s'}}}).fetch()
    Results.find ({'results.errorId.$' : 'uX6byeiuGjRNXTj6s'}).fetch()

Please help.

Thanks!!!

3 Answers 3

1

Please try to do it through aggregation

Results.aggregate([{$unwind: '$results'},
                   {$match: {'results.errorId': 'uX6byeiuGjRNXTj6s'}}])
Sign up to request clarification or add additional context in comments.

1 Comment

Got it. I had to run it on server side.
0

You can use $elemMatch. Mongo documentaton provides examples. But for your case use:

{ results: { $elemMatch: { errorId: "xyz"} } }

If this does not work, according to the mongo documentation you could use:

{ "results.errorId": "xyz" }

Comments

0

I tried to run queries from browsers console and got following results:

    Results.find({ results: { $elemMatch: { errorId: "uX6byeiuGjRNXTj6s"} } }).fetch()
    [Object { _id="9FK5k755ueAYdfip3",  results=[435],  createdAt=Date}]

    Results.find({ results: { $elemMatch: { "results.errorId": "uX6byeiuGjRNXTj6s"} } }).fetch()
    []

    Results.aggregate([{$unwind: '$results'},{$match: {'results.errorId': 'uX6byeiuGjRNXTj6s'}}]).fetch()
    TypeError: Results.aggregate is not a function


    ...gregate([{$unwind: '$results'},{$match: {'results.errorId': 'uX6byeiuGjRNXTj6s'}...

First query returned all ids instead of just "uX6byeiuGjRNXTj6s". Other two queries didnt work. Then I tried to run them directly (meteor mongo) from db and got below results.

     db.results.find({ results: { $elemMatch: { errorId: "uX6byeiuGjRNXTj6s"} } })
      - returned ALL ids

     db.results.find({ results: { $elemMatch: { "results.errorId": "uX6byeiuGjRNXTj6s"} } })
     - returned empty

     meteor:PRIMARY> db.results.aggregate([{$unwind: '$results'},{$match: {'results.errorId': 'uX6byeiuGjRNXTj6s'}}])
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 13:01:29  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 13:01:29" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 13:01:29  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 13:01:29" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 13:22:42  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 13:22:42" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 13:22:43  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 13:22:43" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 13:49:33  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 13:49:33" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 13:49:34  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 13:49:34" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 14:02:22  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 14:02:22" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 14:02:22  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 14:02:22" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 14:30:41  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 14:30:41" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 14:30:41  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 14:30:41" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 15:46:41  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 15:46:41" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/09/15 15:46:42  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/09/15 15:46:42" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 15:26:24  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 15:26:24" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 15:26:24  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 15:26:24" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 15:40:47  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 15:40:47" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 15:40:48  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 15:40:48" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 15:47:49  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 15:47:49" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 15:47:49  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 15:47:49" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 16:08:38  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 16:08:38" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    { "_id" : "9FK5k755ueAYdfip3", "results" : { "errorId" : "uX6byeiuGjRNXTj6s", "error" : "02/10/15 16:08:38  backup.swatycomet.lan nsrd Error encountered while re-signing lockbox 'D:\\EMC\\Networker\\nsr\\lockbox\\backup.comet.si\\clb.lb': The lockbox file was not found. ", "time" : "02/10/15 16:08:38" }, "createdAt" : ISODate("2016-03-12T18:58:46.967Z") }
    Type "it" for more

So here third query returned what I need. Then I tried to run that query in my meteor code:

 console.log(Results.aggregate([{$unwind: '$results'},{$match: {'results.errorId': 'uX6byeiuGjRNXTj6s'}}]));

but get below error instead of results.

Exception in template helper: .errors@http://localhost:3000/app/client/templates/results/results.js?951539bd4f24742e5d7a64530c2463bfe41fde91:33:21 bindDataContext/<@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2986:14

What I am doing wrong?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.