0

I'm working with following document

{
"_id" : 123344223,
"firstName" : "gopal",
"gopal" : [ 
    {
        "uuid" : "123",
        "name" : "sugun",
        "sudeep" : [ 
            {
                "uuid" : "add32",
                "name" : "ssss"
            }, 
            {
                "uuid" : "fdg456",
                "name" : "gfg"
            }
        ]
    }, 
    {
        "uuid" : "222",
        "name" : "kiran"
    }
]
}

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

here is what i have tried

Template.table.helpers({
ProductManager: function () {
return ProductManager.findOne({_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.sudeep}}
<tr>
<td>{{name}}</td>
<td>{{uuid}}</td>
</tr>
{{/each}}
</tbody>
</table>

when i tried this

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

Iam able get this in mongo shell

{
    "_id" : 123344223,
    "gopal" : [ 
        {
            "uuid" : "123",
            "name" : "sugun",
            "sudeep" : [ 
                {
                    "uuid" : "add32",
                    "name" : "ssss"
                }, 
                {
                    "uuid" : "fdg456",
                    "name" : "gfg"
                }
            ]
        }
    ]
}

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

  • This is a duplicate I guess, pls see my answer on the other question here: [Check autopublish](http://stackoverflow.com/a/28361458/4491806) – Meteorpoly Feb 06 '15 at 13:28

2 Answers2

0

Try something like this:

{{#each ProductManager}}
  {{#with this.gopal.sudeep}}
      ...
  {{/with}}
{{/each}}
tarmes
  • 15,366
  • 10
  • 53
  • 87
0

If you know the id of the document you can write this query

ProductManager.find({_id: productID}, {"gopal.sudeep":1});

This query return a cursor with all data only at gopal.sudeep and then you can iterate through this cursor at you template to display name and uuid of all elements

So at js file you can write

Template.table.helpers({
     ProductManager: function () {
       return ProductManager.find({_id: youid}, {"gopal.sudeep":1});
     }
});

And at your template file

<tbody>
  {{#each ProductManager}}
    <tr>
      <td>{{name}}</td>
      <td>{{uuid}}</td>
    </tr>
  {{/each}}
</tbody>

Good luck!

mstamos
  • 111
  • 1
  • 3
  • hey thanks for your reply......but its not working.....can you please suggest me some other way to solve this – Sugun Rajiv Feb 07 '15 at 07:35
  • The query ProductManager.find({_id: youid}, {"gopal.sudeep":1}); what did return – mstamos Feb 07 '15 at 08:36
  • { "_id" : 123, "gopal" : [ { "sudeep" : [ { "uuid" : "add32", "name" : "ssss" }, { "uuid" : "fdg456", "name" : "gfg" } ] }, {} ] } – Sugun Rajiv Feb 07 '15 at 12:20
  • I want my output to be like this{ "_id" : 456, "gopal" : [ { "uuid" : "123", "name" : "sugun","sudeep" : [ { "uuid" : "add32", "name" : "ssss" }] }] } – Sugun Rajiv Feb 07 '15 at 12:28
  • Try this to see if it works ProductManager.findOne({_id: youid})["gopal"]["sudeep"][0]. Print out the correct results? – mstamos Feb 07 '15 at 13:08
  • db.People.aggregate([ {$match: {_id: 123}}, {$unwind: "$gopal"}, {$unwind: "$gopal.sudeep"}, {$match: {"gopal.uuid": "123", "gopal.sudeep.uuid" : "add32"}} ]) I tried this and succeded....Thanks for your response – Sugun Rajiv Feb 09 '15 at 06:03