Sendgrid templates with test data and arrays - sendgrid

I have some test data that looks like this:
{
"firstName":"Ben",
"products": [{
"name": "first product",
"position": 0
}, {
"name": "second product",
"position": 1
}, {
"name": "third product",
"position": 2
}]
}
In my template, I have something like this:
<p>Hi {{firstName}}</p>
Which works fine.
Now I want to loop through my products
So I tried this:
{{#each products}}
{{#if this.position == 0}}
<h1>{{this.name}}</h1>
{{else}}
<h2>{{this.name}}</h2>
{{/if}}
{{/each}}
But it doesn't work.
I also can't find any documentation about doing if statements like that. The closest I found was:
https://sendgrid.com/docs/ui/sending-email/using-handlebars/
And it talks about "Basic If, Else, Else If" which suggests there is a more advanced version, but I can't find the documentation for it....
Does anyone know what I am doing wrong?
PS: my examples are simplified just for this post.

It looks like the SendGrid documentation is just using Basic If... to differentiate from the next section of If with a Root. They used similar titles in sections higher up the page.
Based on that, it doesn't appear that they have logic for "if value equals", just "if field exists", so you'd need to update your JSON to use that format, such as they have in later examples.

What you need is the following construct:
{{#equals city_code avl}}
AVL
{{else}}
Not AVL
{{/equals}}
See https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements.
{{if anything}} only checks whether anything is truthy or falsy; you cannot compare it to something.

Related

MongoDB search multiple Field in multiple subDocument

My MongoDB data structure looks like this:
{
"id": "7118592",
"passages": [
{
"infons": {
"article-id_pmid": "32292259",
"title": "Keywords",
"type": "front",
"section_type": "TITLE"
},
"text": "Understanding of guidance for acupuncture and moxibustion interventions on COVID-19 (Second edition) issued by CAAM"
},
{
"infons": {
"section_type": "ABSTRACT",
"type": "abstract",
"section": "Abstract"
},
"offset": 116,
"text": "At present, the situation of global fight against COVID-19 is serious. WHO (World Health Organization)-China Joint Mission fully confirms the success of \"China's model\" against COVID-19 in the report. In fact, one particular power in \"China's model\" is acupuncture and moxibustion of traditional Chinese medicine. To better apply \"non-pharmaceutic measures\":the external technique of traditional Chinese medicine, in the article, the main content of Guidance for acupuncture and moxibustion interventions on COVID-19 (Second edition) issued by China Association of Acupuncture-Moxibution is introduced and the discussion is stressed on the selection of moxibustion device and the duration of its exertion."
}
]
}
I want to request the article-id_pmid and the text in the same subdocument, also the text of subdocument which contains in infons a field with section_type : ABSTRACT and type: abstract.
I had tried this request but the result was not what I am searching for:
db.mydata.find({$and:[{"passages.infons.article-id_pmid$":{$exists: true}},{"passages.infons.section_type":"ABSTRACT"},{"passages.infons.type":"abstract"}]},{"passages.infons.article-id_pmid.$":1,_id:0, "passages.text":1})
Each of the top-level conditions is treated independently.
Use https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ to specify multiple conditions on the same array element.

Mongoose Model.deleteMany() only deletes first element of matches

I'm trying to use the Model.deleteMany() function from mongoose. I'm trying to do something like this:
MyModel.deleteMany({"_id": {$in: ['objectid 1', 'objectid 2'
But this only deletes the first element of the matches from DB (in this case, if 'objectid 1' exists in the DB, it deletes that, but if it isn't nothing happens (both n and deletedCount is 0 in the returned data from the function). I tried using something other than the _id as a query, and this worked. If I had three elements with the same 'name' field, I could delete these.
I tried _id both with and without quotation marks. I also tried converting the object id strings to actual object ids before passing them to deleteMany, but this had no difference either. I have also of course tried to google this, but everything I've found are examples of usage, where it looks like I'm doing the exact same thing as the various blog posts.
I haven't added much code here because I don't really see what else I could be adding. I'm already printing out the input to the $in object, and this is correct. The strangest thing, I think, is that the first element of the list is deleted. Is it treated as a deleteOne request for some reason? are there any config options I need?
As per request, I've added the query and the documents I'd hope to delete:
//Request
MemberModel.deleteMany({"_id": {$in: [
5ee4f6308631dc413c7f04b4,
5ee4f6308631dc413c7f04b5,
5ee4f6308631dc413c7f04b6
]}};
//Expected to be deleted
[
{
"_id": "5ee4f62f8631dc413c7f04b5",
"firstName": "Name",
"lastName": "Nameson",
"__v": 0
},
{
"_id": "5ee4f62f8631dc413c7f04b6",
"firstName": "Other",
"lastName": "Person",
"__v": 0
}
]
If you have any ideas for what I could try, that would be much appreciated.

How do I auto insert a timestamp, into my document when a document is created / edited?

I'm just getting started with mongoDB, and coming from a MySQL environment I'm having trouble figuring out how have my documents automatically have fields such as UPDATED, CREATED, DELETED
So when I create an entry like this:
{
"email": "some#test.com",
"name": "bob"
}
I would like it to automatically become this:
{
"email": "some#test.com",
"name": "bob",
"CREATED": 1567120458,
"UPDATED": 1567120458,
"DELETED": null
}
I have found https://docs.mongodb.com/manual/reference/operator/update/currentDate/ this resource which talks about a way of inserting dates, but I am not sure how to use this, or where to place this.
{ $currentDate: { <field1>: <typeSpecification1>, ... } }
I don't know what this piece of code means or where to use it.
I've installed Stuido 3T to help me manage the database, but I don't see any option to use this piece of code.
Construct your query use Date:
db.example.insert({"date":new Date(Date.now())})

get the particular field value from mongodb

In mongodb i saved document like this.
"Angela_Merkel": {
"birthPlace": "Hamburg,West_Germany",
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Angela-Merkel-2014.jpg?width=300",
"almaMater": "Leipzig_University",
"birthDate": "1954-07-17",
"spouse": "Joachim_Sauer"
}
There are many person's information like this way. Now if I want to get all the information of "Angela_Merkel" or only a particular like "birthdDate" of "Angela_Merkel" then what will be the query?
Like chridam says would be more practical that you refactor your documents like this:
{"name": "Angela_Merkel",
"birthPlace": "Hamburg,West_Germany",
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Angela-Merkel-2014.jpg?width=300",
"almaMater": "Leipzig_University",
"birthDate": "1954-07-17",
"spouse": "Joachim_Sauer"
}
Inside the "people" collection (Its a convention name the collections as plurals, beeing a set of documents)
Then you could do:
db.people.find({"name":"Angela Merkel"},{"_id":0," "birthdDate":1 })
To get:
{"birthDate": "1954-07-17"}
Anyway you can get with your document what you want this way:
"birthDate":
db.person.find({"Angela_Merkel.birthDate": "1954-07-17"})
or all the info:
db.person.find({"Angela_Merkel": { $exists: true}})
But doesn't have much sense do it this way.
PD: Edited to include last part.

CouchDB Group Level and Key Range

Can anyone explain to me why the following doesn't work:
Assuming the following document structure:
{
"_id": "520fb089a6cb538b1843cdf3cca39a15",
"_rev": "2-f96c27d19bf6cb10268d6d1c34799931",
"type": "nosql",
"location": "AZ",
"date": "2012/03/01 00:00:00",
"amount": 1500
}
And a Map function defined like so:
function(doc) {
var saleDate = new Date(doc.date);
emit([doc.location,saleDate.getFullYear(),saleDate.getMonth()+1],doc.amount);
}
And using the built in _sum function for the reducer.
When you execute this (with group=true) you get results like this:
{"rows":[
{"key":["AZ",2012,2],"value":224},
{"key":["AZ",2012,3],"value":1500},
{"key":["WA",2011,12],"value":1965},
{"key":["WA",2012,1],"value":358}
]}
Now if you change the query to something like this:
http://127.0.0.1:5984/default/_design/nosql/_view/nosql_test?group_level=2
You get results like this:
{"rows":[
{"key":["AZ",2012],"value":1724},
{"key":["WA",2011],"value":1965},
{"key":["WA",2012],"value":358}
]}
So with that in mind if I wanted to find out all sales in 2011 for "WA" could I not execute something like this:
http://127.0.0.1:5984/default/_design/nosql/_view/nosql_test?group_level=2&key=["WA",2011]
This example has been taken from the useful videos over at NoSQL tapes.
http://nosqltapes.com/video/understanding-mapreduce-with-mike-miller
You always need to give a range of keys, because filtering is done on map's results, not on reduce.
For example, the following parameters should work (if properly url-encoded):
?group_level=2&startkey=["WA",2011]&endkey=["WA",2011,{}]
You can read about view collation to understand how it works.