Display amount of comments from different database table - angularjs

Before I start coding I'm trying to figure out how I'd do it first. So what I want to do is to display a list of articles, by simply loading it from a database for example: getAllArticles().
The list will display all the titles from the articles. Nothing difficult about that.
However, there's a chance to comment on that article so all the comments will be stored in a new database tabel with the ID of the article.
I'll probably have to load them with getAllCommentsById() but how do I display the amount of comments next to each article in an ng-repeat?
I don't have any code yet, but let's say it will look like this:
Angular
<div ng-repeat='article in articles'>
<p>{{amount}}</p>
<p>{{article.title}}</p>
</div>
$scope.articles = getAllQuestions();
Article database
{
"id":1,
"title": "Article 1"
}
{
"id":2,
"title": "Article 2"
}
Comments database
{
"id":1,
"comment": "Lorem ipsum",
"article_id": 1
},
{
"id":2,
"comment": "Lorem ipsum",
"article_id": 1
},
{
"id":3,
"comment": "Lorem ipsum",
"article_id": 2
}

I think that you can do it in two several ways.
You can add a counter of the comment relatives to an article in the articles object
Create a function in angular's controller that give the number of comments of an article
$scope.countComments = function(articleId) {
...
}
Create an angular service that count the comments of each article

Related

MongoDB - Update multiple subdocuments in array in multiple documents

I'm making a node.js website. I have a posts collection in which comments for posts are stored in an array with the comment author's details as nested object.
This is new post's schema:
{
"text": text,
"image": image,
"video": video,
"type": type,
"createdAt": createdAt,
"reactions": [],
"comments": [],
"shares": [],
"user": {
"_id": user._id,
"username": user.username
}
}
This is new comment being pushed to its post:
$push: {
"comments": {
"_id": commentId,
"user": {
"_id": user._id,
"type": type,
"name": user.name,
"profileImage": user.photo,
},
"comment": comment,
"createdAt": createdAt,
"replies": []
}
}
To avoid storing comments in another collection and doing complex multiple lookups(I'm doing 1 lookup to get post author details but couldn't add another to make it work for comments) to consolidate the newsfeed I decided to save comments and their author's details embedded in the posts.
Now when user profile picture is updated all the comments have to be updated to show the new picture.
I included this updateMany query along with the photo updation route in server.js file:
database.collection("posts").updateMany({
"comments.user._id": user._id,
"comments.user.type": "friend"
}, {
$set: {
"comments.$.user.profileImage": photo
}
});
The problem here is that this updates only the first matching comment in all posts.
I need to update all matching comments in all posts.
I'm actually just learning by doing this following youtube videos, so please help me.
You need to use arrayFilters I think.
If I've understand well your question this example should be similar to your DB.
The query is this:
db.collection.update({
"comments.user._id": 1,
"comments.user.type": "friend"
},
{
"$set": {
"comments.$[element].user.profileImage": "new"
}
},
{
"arrayFilters": [
{
"$and": [
{
"element.user._id": 1
},
{
"element.user.type": "friend"
}
]
}
],
"multi": true
})
First part is the same, and almost the second. You have to add element position into the array that is defined in the next step.
Using arrayFilters, you look for those whose match the comaprsion into $and. And only those ones will be updated.
Note that using updateMany() method, is not neccesary using {multi: true}

What JSON-LD structured data to use for a multi-pararaph, multi-image blogpost?

I have created the following JSON-LD for a blogpost in my blog:
{
"#context": "http://schema.org",
"#type": "BlogPosting",
"mainEntityOfPage": {
"#type": "WebPage",
"#id": "https://www.example.com"
},
"headline": "My Headline",
"articleBody": "blablabla",
"articleSection": "bla",
"description": "Article description",
"inLanguage": "en",
"image": "https://www.example.com/myimage.jpg",
"dateCreated": "2019-01-01T08:00:00+08:00",
"datePublished": "2019-01-01T08:00:00+08:00",
"dateModified": "2019-01-01T08:00:00+08:00",
"author": {
"#type": "Organization",
"name": "My Organization",
"logo": {
"#type": "ImageObject",
"url": "https://www.example.com/logo.jpg"
}
},
"publisher": {
"#type": "Organization",
"name": "Artina Luxury Villa",
"name": "My Organization",
"logo": {
"#type": "ImageObject",
"url": "https://www.example.com/mylogo.jpg"
}
}
}
Now, I have some blog posts that contain multiple paragraphs and each paragraph is accompanied by an image. Any ideas how can I depict such a structure with JSON-LD?
Background
I have created a simple blog which uses a JSON file for 2 purposes: (a) feed the blog with posts instead using a DB (by using XMLHttpRequest and JSON.parse) and (b) to add JSON-LD structured data to the code for SEO purposes.
When I read the JSON file I have to know which image belongs to which paragraph of the text in order to display it correctly.
Note: As you seem to need this only for internal purposes, and as there is typically no need to publically provide data about this kind of structure, I think it would be best not to provide public Schema.org data about it. So you could, for example, use it to build the page, and then remove it again (or whatever works for your case). Then it would also be possible to use a custom vocabulary (under your own domain) for this, if it better fits your needs.
You could use the hasPart property to add a WebPageElement for each paragraph+image block.
Each WebPageElement can have text and image (and, again, hasPart, if you need to nest them).
Note that JSON-LD arrays are unordered by default. You can use #list to make it ordered.
"hasPart": { "#list":
[
{
"#type": "WebPageElement",
"text": "plain text",
"image": "image-1.png"
},
{
"#type": "WebPageElement",
"text": "plain text",
"image": "image-2.png"
}
]
}
For the blog posting’s header/footer, you could use the more specific WPHeader/WPFooter instead of WebPageElement.

Query an array of users based on an array of users

Basically I'm having trouble understanding how I would figure this out.
I have a document in a mongodb collection, and that document has field called friends which is an array of usernames.
I want to query through each username in the array friends, and have an array of those user documents. I'm terrible at explaining maybe if I draw this out it'll make sense.
mongodb document:
{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Nick",
"friends": ["Jones","Mark","Mike"]
}
Now with this friends array, I want to search the same collection for an object with the "name" Jones, Mark, and Mike. When I find that object, I want to put it into an array.
Basically I want it to return this, (for this example let's say Jones, Mark, and Mike only have one friend, and that friend is Nick.
[{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Jones",
"friends": ["Nick"]
},
{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Mark",
"friends": ["Nick"]
},
{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Mike",
"friends": ["Nick"]
}]
^ an array of three objects, which are all the friends of Nick.
If you need any more explanation please let me know, I'm terrible at this type of stuff.
For the record, I'm using node, and basic mongodb (not mongoose).
I believe you are looking for $in operator.
// doc.friends = ["Jones","Mark","Mike"]
db.collection.find({ name: { $in: doc.friends }})

JSON data comparison in Angularjs

I am doing a college project. In category page there will be product categories coming from json-
[{
"code": "mc",
"name": "Mobile",
"desc": "We provide range of latest mobile phones with best price in the market",
"imageUrl": "images/mobile.jpg"
},
{
"code": "lc",
"name": "Laptops",
"desc": "Huge sale is going on Lenova,HP and Apple's laptop",
"imageUrl": "images/lappy.jpg"
},
{
"code": "ic",
"name": "ipads",
"desc": "ipad mini is available with the cheapest price.Go grab the offer",
"imageUrl": "images/ipad.jpg"
},
{
"code": "sc",
"name": "Storage Devices",
"desc": "All kind of storage devices are available here",
"imageUrl": "images/storage.jpg"
}
]
As the category list is dynamic for eg 4, trying to build up link to list page depends on the category based on code data in JSON. For example if click on mobile in category.html, result.html will be mobile list, if iPad then iPad list. Right now category page done and trying to pass and receive code in JSON to compare in condition to display concerned data like laptop or desktop.
Present js is:
var catApp = angular.module("catApp", ["ngStorage"]);
catApp.controller('CountryCtrl', function ($scope, $http, $localStorage){
$http.get('categories.json').success(function(data) {
$scope.categories = data;
});
$scope.save = function() {
window.location.replace('result.html');
$scope.type=categories;
if(type.code=='mc'){
localStorage.setItem('msg1', 'Mobiles');
}
$scope.data1 = localStorage.getItem('msg1');
});
to display it on result.html-
<body ng-app="catApp">
<div ng-controller="CountryCtrl">
{{data1}}
</div>
</body>
Please help me to display product name in result.html comparing the 'code' from the json.
In short want to display category name in result.html depends on which category user click where category also dynamic coming from json with a fag value code to make it work like-
freshtechsample.tk/pal/test.html
Only difference is in above html links are static, in required page links are dynamic.
Present output:http://freshtechsample.tk/pal/category.html

How do I get and set model data that's nested deep in Backbone?

I have this model and I want to update the team value when I gather the correct info on a form submit but I'm not sure how to the specific team. I know in backbone I can use the model to get teams but I'm not sure how to then go from teams into the correct team using the code attribute?
I've tried:
// My new team to replace team in code EFU"
var newTeam: {
"id": "POS-876",
"name: "Swansea City"
}
var teams = this.model.get('teams');
var selectedTeam = teams.get('EFU'); // I know this is wrong
selectedTeam.set('team', newTeam);
I'm not sure how I target the correct team then update the team details?
Model
{
"id": "4V6",
"name": "Premier League",
"teams": [{
"code": "EFU",
"team": {
"id": "POS-1",
"name": "Manchester United"
}
}, {
"code": "BMD",
"team": {
"id": "POS-223",
"name": "Chelsea"
}
}]
}
Your Model is a bit confusing. Why is it wrapped in array brackets? And why are you getting 'exchanges' instead of 'teams'? And should perhaps teams be a collection instead of a simple array?
But in any case, to find the specific team, you can use Underscore.
var selectedTeam = _.findWhere(teams, {code: 'EFU'});

Resources