How do I query nested object by property inside an array?

徘徊边缘 提交于 2019-12-12 17:20:26

问题


I have a collection of several thousand (potentially 30-40k) documents, with a structure (greatly simplified) like this:

{
 propA:'123',
 obj: [
       {prop1:'a'},
       {prop1:'b'},
       {prop1:'c'}
 ],
 propB:456
}

How can I query to find all documents where obj.prop1==='b'? I can't seem to figure out how to check a property of an object inside an array property. This is using version 1.5.1.

Thanks!

Update I've done some digging into MongoDB style queries (which I believe Loki emulates?). The following works on Mongo, but not in Loki: myCollection.find( { "obj.prop1": "b" } ). Loki give me an error: Uncaught SyntaxError: Unexpected token .

Update 2 Resolved - see my answer...


回答1:


I needed to include the nested property name in quotes in my query. The query example I tried in Mongo does actually work in Loki if you include the object.propName in quotes, so this DOES work: myCollection.find( { "obj.prop1": "b" } )



来源:https://stackoverflow.com/questions/48414562/how-do-i-query-nested-object-by-property-inside-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!