简体   繁体   English

如何通过使用嵌入式文档的值有效地从JSON文档数组中访问JSON文档?

[英]How do I efficiently access JSON documents from an array of JSON documents, by using value of embedded document?

I need a method/strategy in JavaScript to access documents from a JSON array. 我需要JavaScript中的方法/策略来从JSON数组访问文档。 I need something that works kind of like $elemMatch in MongoDB Shell. 我需要像MongoDB Shell中的$ elemMatch这样的东西。

The data is in the following format: 数据格式如下:

[
    { 
      a : '1',
      b : '2',
      c : '3'
    },
    { 
      a : '4',
      b : '5',
      c : '6'
    },
    {...},
    {...}
]

So all the documents have the same keys and I need to access the 'c' value when I have the 'a' value. 因此,所有文档都具有相同的键,当我具有“ a”值时,我需要访问“ c”值。

Is any way to do this efficiently in JavaScript, ie without having to loop through the entire array? 有没有办法在JavaScript中有效地做到这一点,即不必遍历整个数组? This will form part of a much larger query function and performance is extremely important. 这将构成更大的查询功能的一部分,性能非常重要。

You can use the find command with nested data. 您可以将find命令与嵌套数据一起使用。 Check this link for javascript syntax and the accepted answer in this link for a general idea on what you're trying to do. 请查看此链接以获取javascript语法以及此链接中可接受的答案,以大致了解您要执行的操作。

Edit: For javascript, you could try to use the match command with the aggregation pipeline framework ( more info here ). 编辑:对于javascript,您可以尝试将match命令与聚合管道框架一起使用( 更多信息在此处 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM