Gopal Rao
Gopal Rao

Reputation: 161

find sub array documents in meteor

I'm working with following document

{
"_id" : 123344223,
"firstName" : "gopal",
"gopal" : [ 
{
 "uuid" : "123",
 "name" : "sugun",


"kiran" : [ 
{
 "uuid" : "456",
 "name" : "ghb"
}

       ]
}
]
}

I want to retrieve name from the first document of array kiran and print it in a table...

here is what i have tried

Template.table.helpers({
ProductManager: function () {
return ProductManager.find({_id:123344223},{gopal:{$elemMatch:{uuid:"123"}}});
}
 })    

where ProductManager is my collection and defined in common.js

ProductManager = new Meteor.Collection("ProductManager"); 

Here is my template

<template name="table">
<table>
<thead>
<tr>
<th>NAME</th>
<th>UUID</th>
</tr>
</thead>
<tbody>
{{#each ProductManager.gopal.kiran}}
<tr>
 <td>{{name}}</td>
 <td>{{uuid}}</td>
 </tr>
 {{/each}}
 </tbody>
 </table>

when i tried this

ProductManager.find({_id:123344223},{gopal:{$elemMatch:{uuid:"123"}}}); 

Iam able get this in mongo shell

{
"_id" : 123344223,
"firstName" : "gopal",
"gopal" : [ 
{
 "uuid" : "123",
 "name" : "sugun",


"kiran" : [ 
{
 "uuid" : "456",
 "name" : "ghb"
}

       ]
}
]
}

but cant print name and uuid of Kiran array in the table....... plzz help me to solve this problem... Thanks in advance

Upvotes: 1

Views: 479

Answers (1)

Meteorpoly
Meteorpoly

Reputation: 938

  1. Check your json format as it doesn't look valid to me
  2. Check if autopublish / insecure is missing from your meteor project, if yes implement pub/sub functionality

Discovermeteor has a good guide for that Discovermeteor

Another stackoverflow post for sub-document searches How to write Mongo query to find sub document with condition

Upvotes: 0

Related Questions