Mongo查询从collections数组中返回特定值

时间:2016-03-13 12:20:59

标签: javascript mongodb meteor

我的下面是名为Results的集合。

IDictionary<int, string> a = new ConcurrentDictionary<int, string>(); // works
IReadOnlyDictionary<int, string> b = new ConcurrentDictionary<int, string>(); // does not work

我需要查询,它将返回数组结果中特定errorId的所有文档。

我尝试了不同的查询但没有任何效果。

例如:

_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"}

请帮忙。

感谢!!!

3 个答案:

答案 0 :(得分:1)

请尝试通过aggregation

进行操作
Results.aggregate([{$unwind: '$results'},
                   {$match: {'results.errorId': 'uX6byeiuGjRNXTj6s'}}])

答案 1 :(得分:0)

您可以使用$elemMatch。 Mongo documentaton提供examples。但是对于你的案例使用:

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

如果这不起作用,根据您可以使用的mongo文档:

{ "results.errorId": "xyz" }

答案 2 :(得分:0)

我尝试从浏览器控制台运行查询并获得以下结果:

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

第一个查询返回所有ID,而不仅仅是“uX6byeiuGjRNXTj6s”。其他两个查询没有用。 然后我尝试从db直接运行它们(meteor mongo)并得到以下结果。

     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

所以这里第三个查询返回了我需要的内容。然后我尝试在我的流星代码中运行该查询:

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

但要低于错误而不是结果。

模板助手中的异常:.errors @ http://localhost:3000/app/client/templates/results/results.js?951539bd4f24742e5d7a64530c2463bfe41fde91:33:21 bindDataContext /&LT; @ http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2986:14

我做错了什么?