Show data in meteor-template from array in - meteor

I have restaurants in a MongoDB with this structure
"_id" : "R68ZkDqdfj7Qsc9Kx",
"clubname" : "Italiano",
"description" : "Best italian food in town.",
"capacity" : "100",
"createdAt" : ISODate("2016-04-13T16:20:20.683Z"),
"visitors" : [
{
"persons" : 0
}
],
"createdBy" : "mFWrAd3SdyP4dRgEW"
I have no problems retrieving the name, description, capacity... data, but I can't figure out how to read the value 0 in the array under visitors.
In MongoDB I can do:
db.clubs.findOne({_id: "R68ZkDqdfj7Qsc9Kx"}).visitors[0].persons
But how do I do something similar in the Meteor template?
{{visitors}} returns [object Object]
I have also tried
{{#with visitors}}

visitors is an array, so you you'll probably want to iterate over it and show your data in a list. Here's an example:
<ul>
{{#each visitors}}
<li>Persons: {{persons}}</li>
{{/each}}
</ul>

Related

Cloud Firestore : Query nested map of arrays

My firestore document structure looks like this
doc1
{
name: 'Test',
categories: [
{
allNames: ['name1', 'name2', 'name3],
id: 'abcd'
},
{
allNames: ['name4', 'name5'],
id: 'xyz'
},
]
doc2
{
name: 'Test2',
categories: [
{
allNames: ['name7'],
id: 'abcd3'
},
{
allNames: ['name4', 'name5'],
id: 'xyz'
},
]
I am using the JS SDK and wanted to query all document with a certain category(id or name). So if I query with categories.id equal to xyz or categories.allNames contains name1, both the above documents should be returned.
Are both these queries possible or do I need to remodel my data ?
Thank you !
You cannot query arrays that way. If you know the exact object then you could use array-contains as shown below:
firestore.collection('col').where('categories', 'array-contains', {id: 'abc', allNames: ['name1']})
One solution would be to store another array which contains category IDs only like this:
{
name: 'Test1',
categories: [...],
categoryIds: ['abcd', 'xyz']
}
Now you can use arrayContains even if you know the category ID only and not the whole object in categories array. The same method can be used for allNames.
Categories can also be made a sub-collection where each object in categories array would be a document. Then you can use Collection Group queries to find category documents with that ID or allNames and find parent document ID using the DocumentReference.

Handlebars lookup object using key from array?

I have an object with an array and an object. I iterate the array (fields) - using it as a template to create form elements. I want to - in the process - get the corresponding object value from the object (data).
{
"fields": [
{
"name": "id",
"type": "int",
"max_length": 11,
},
{
"name": "email",
"type": "varchar",
"max_length": 191,
}
],
"data": {
"id": "4",
"email": "person#domain.com",
}
}
Something like this (mind map):
{{#each fields}}
<li>
lookup {{lookup ../data => VALUE OF CORRESPONDING KEY ("id" or "email" etc.)}}
<label for="{{name}}">{{CamelCase name}}</label>
{{InputType name type}}
</li>
{{/each}}
So, when the field name is 'id' I'd like to grab the value of 'id' from the object etc.
I can't seem to wrap my head around the lookup ... or are there some other more clever and direct way of achieving this?
I think you summarized the desired logic very well when you said "when the field name is 'id' I'd like to grab the value of 'id' from the object".
In other words, you want to perform a lookup on the data Object and the value of the name variable is the key to be looked up.
The lookup would be simply: {{lookup ../data name}}. name will evaluate to the desired key to be looked-up on the context object ../data.
Thanks, ended up doing this (passing the data to the helper):
{{#each fields}}
<li>
<label for="{{name}}">{{CamelCase name}}</label>
{{InputType name type ../data }}
</li>
{{/each}}
And then in the helper extracted the value by doing:
Handlebars.registerHelper("InputType", function ( name, type, value) {
switch ( type ) { …
… value="' + value[name] + '">');

Structure Products & Categories - DynamoDB

I am creating a product catalogue database with 10000s of products and wondered whats the best way to structure them in a NoSQL database?
I need to make sure that i can create a category structure to build the menu on the fly (which will be cached daily)
First I thought that the products could contain their own category data
EAN is the Primary Key
CatSlug is the Sort Key
Product json
Products: [
 {
"EAN" : {"S" : "123456"},
"CatSlug" : {"S" : "drill"},
"CatParent" : {"S" : "drill"},
"Name" : {"S" : "Drill"},
"Img" : {"S" : "/img/img.png"},
"Desc" : {"S" : "Description"}
....
},
{NEXT PRODUCT}
]
But doing this would be difficult to create a menu structure.
Then I though having the CatSlug as the PK would be better to create the menu structure, but you would still need to go through loads of items to create this.
Having it as a separate table is quicker, but seems wrong for this type of database.
Is there a good way to do this in Dynamo DB?
You can express your category hierarchy in CatSlug using this pattern: cat#cat#cat...
then build a secondary global index with CatSlug as PK, and EAN as SK.

Angularfire: how to query the values of a specific key in an array?

I have the following json object:
{
"posts" : {
"-Jk3ipZ2EFgvkABHf9IX" : {
"category" : "JavaScript",
"date" : 1426008529445,
"heading" : "Lorem",
"text" : "dsvgfjds daefsrgfs defsds dsfsdds",
"user" : "Mitko"
},
"-Jk3jFbOOE9V8Yd37tWj" : {
"category" : "C#",
"date" : 1426008640253,
"heading" : "Lorem",
"text" : "Some Text!",
"user" : "Peter"
}
}
}
I'm using Angularfire for my app. Is there a way for me to query this json and just extract the category names or am I supposed to create an additional object for the categories and query that way. I am asking because it feels redundant to query the whole array for just one value.
Thanks in advance
Firebase's JavaScript SDK (on top of which AngularFire is built) will always load complete nodes. So there is no way to load just the category from your posts.
The common way around this is to create a second top-level node that contains just the categories; or alternatively the categories and (for each) the push-id of each post in that category.
{
"posts" : {
"-Jk3ipZ2EFgvkABHf9IX" : {
"category" : "JavaScript",
"date" : 1426008529445,
"heading" : "Lorem",
"text" : "dsvgfjds daefsrgfs defsds dsfsdds",
"user" : "Mitko"
},
"-Jk3jFbOOE9V8Yd37tWj" : {
"category" : "C#",
"date" : 1426008640253,
"heading" : "Lorem",
"text" : "Some Text!",
"user" : "Peter"
}
},
"categories" : {
"JavaScript" : {
"-Jk3ipZ2EFgvkABHf9IX"
},
"C#" : {
"-Jk3jFbOOE9V8Yd37tWj"
}
}
It is a bit redundant, but allows you to get exactly the data you need to show a specific screen in your UI. Denormalizing your data like this is a common part of transitioning to a NoSQL database.

How to display all data in the database into twig?

Update : As Gerry suggested i fix the problem but i can't display nothing using twig. I dump the variable and this is what i get:
object(Acme\Bundle\NewBundle\Entity\database)#275 (10) {
["id":"Acme\Bundle\NewBundle\Entity\database":private]=> int(2)
["username":"Acme\Bundle\WebBundle\Entity\database":private]=> string(4) "almighty"
["password":"Acme\Bundle\NewBundle\Entity\database":private]=> string(7) "blabla"
["from":"Acme\Bundle\NewBundle\Entity\database":private]=> string(6) "bitola" ....
I have tried with:
{{% for i in user %}}
{{i.username}} - username
{{i.password}} - password
{{% endfor %}}
Still no luck .. the variable has data in it, but it is an object and not an array.So how can i display is ?
Thanks.
You just need to assign the result of the find() method to your template. find() returns the resulting object (if a match).
$user = $this->getDoctrine()
->getRepository('AcmeNewBundle:database')
->find($username);
return $this->render('AcmeNewBundle:Default:hello.html.twig',array('user' => $user));
If your user object has a property username, you can display it in your template like so:
{{ user.username }}

Resources