Inject data from MongoDB into chart.js - node.js

I'm building a little app and I need to inject data from MongoDB which looks like this :
And there is my chart which looks like this :
labels : ['label1', 'label2', 'label3'],
datasets: [ {
data: [ 170.3, 108.75, 35.3, 37.98 ]
} ]
What I would like is :
In labels : It needs to contain all the "titre" data ( See the picture ) , if one "titre" is used more than once, it needs to display only one
In data, it needs to add every items from the "montant" ordered by category
I'm sure nobody will understand 🤪 Sorry
Example : They are 3 documents with the "titre" groceries.
The "montant" are 4, 7 , 12 So the chart needs to look like this :
labels : ['Groceries', 'label2', 'label3'],
datasets: [ {
data: [ 23, ..., ... ]
} ]
How can I achieve this in the component.ts ?
Thank you everyone !

Related

Fetched sorted API data(NodeJs &Mongoose) not getting displayed in sorted order when try display in Angular UI

I have tried to get sorted in backend & tested via postman and I am getting sorted order.
const locationInfo = await locationDetails.find(query).sort({sectionName:1});
res.json(locationInfo);
[
{ //some other keys &values
"sectionName": "Closet",
},
{
"sectionName": "Dining",
},
{
"sectionName": "Kitchen",
},
{
"sectionName": "Other",
},
{
"sectionName": "Refrigerator",
}
]
After REST call storing result to,
this.result=data;
but when I try to display the same resultant data on UI, Its not getting displayed in sorted order as well as checked in console also resultant data order got changed.
Console Data
[{
sectionName: "Refrigerator",
},
{
sectionName: "Kitchen",
},
{
sectionName: "Dining",
},
{
sectionName: "Closet",
},
{
sectionName: "Other",
}]
Note: Tried to sort from .ts file also but it is not working.
this.result.sort(function(a,b){a.sectionName-b.sectionName});
If any help would be appreciated. Thanks!
SectioName is not a valid criterion for MongoDB to sort the return result. In this case, MongoDB does not know how to sort it.
Here is an example directly from the MongoDB documentation about cursor.sort():
db.restaurants.insertMany( [
{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
] );
# The following command uses the sort() method to sort on the borough field:
db.restaurants.find().sort( { "borough": 1 } )
Documents are returned in alphabetical order by borough, but the order of those documents with duplicate values for borough might not be the same across multiple executions.
.sort works best with numerical values. If you are in control of the backend and are able to change how data is stored in the database. I suggest you create a field for the creation date or just an index to indicate the order of the items.
Let's say your document looks something like this:
# Doc 1
{
sectionName: "Refrigerator",
order:1
}
# Doc 2
{
sectionName: "Refrigerator",
order:2
}
Then you can do
const locationInfo = await locationDetails.find(query).sort({order:1});
which will return you the documents sorted using the order field, and the order will be consistent.

PySpark: Formatting JSON before input to DataFrame

I want to create a Spark dataframe which contains a list of labelled tweets from a number of separate JSON files. I've tried simply using spark.read.json(files, multiLine=True) but I end up with a _corrupted_record in some files, there's something Spark doesn't seem to like about the format (JSON is valid, I've checked).
The following is a representation of the format of each JSON object per file that I'm dealing with:
{"annotator": {
"eventsAnnotated" : [ {...} ],
"id" : "0939"
},
"events": [
{"eventid": "039393",
"tweets": [
{
"postID" : "111",
"timestamp" : "01/01/01",
"categories" : [ "Category" ],
"indicatorTerms" : [ ],
"priority" : "Low",
"text" : "text"
},
...]
However, I'm only interested in the tweets section of the JSON and can disregard eventid, or anything included in annotator:
"tweets": [
{
"postID" : "111",
"timestamp" : "01/01/01",
"categories" : [ "Category" ],
"indicatorTerms" : [ ],
"priority" : "Low",
"text" : "text"
},
...]
I'd like that to end up in a Spark dataframe in which postID, timestamp, categories, indicatorTerms, priority, and text are my columns and each row corresponds to one of these JSON entries.
I guess what I'm asking is how can I read these files into some sort of temporary structure where I can stream, line-by-line, each tweet and then transform that into a Spark dataframe? I've seen some posts about RDDs but only managed to confuse myself, I am pretty new to Spark as a whole.

mongoose, nodejs - add reference of current schema object to the previous schema object

I am using mongoose, nodejs with MVC architecture.
So, I have two collections crops and pesticides. I want a many to many relationship between these two collections.
For example, if I have 2 crops like below:
{
"_id" : ObjectId("5af1d1d54558fae1d0010bb4"),
"nameOfCrop" : "Tomato",
"imageOfCrop" : "tomatoimage",
"soilType" : " almost all soil types except heavy clay",
"waterNeeded" : "water once every two or three days",
"tagCrop" : "Vegetables",
"pesticideForCrop" : [ ]
}
{
"_id" : ObjectId("5af1d1d54558fae1d0010bb5"),
"nameOfCrop" : "Brinjal",
"imageOfCrop" : "brinjalimage",
"soilType" : "all types of soil varying from light sandy to heavy clay",
"waterNeeded" : "Regularly irrigated",
"tagCrop" : "Vegetables",
"pesticideForCrop" : [ ]
}
and two pesticides like below:
{
"_id" : ObjectId("5af7d3e735d4222b78a93838"),
"cropForPesticide" : [ ],
"nameOfPesticide" : "pesticide8",
"imageOfPesticide" : "p8image",
"__v" : 0
}
{
"_id" : ObjectId("5af7d49122b63e0824ed2d3d"),
"cropForPesticide" : [ ],
"nameOfPesticide" : "pesticide9",
"imageOfPesticide" : "p9image",
"__v" : 0
}
What I want is that tomato's pesticideForCrop key have object ids(suppose) of the pesticide pesticide8 and pesticide9 (meaning tomato can be treated with pesticide8 and pesticide9) and simultaneously I want a reference(_id) of tomato in the pesticide8's cropForPesticide key and pesticide9's cropForPesticide key.
I have a very vague approach in mind like firstly, I save a crop with the pesticideForCrop key being null at this point. Then I save a pesticide and while saving it, I can ask the user to select the crops which can be treated with that pesticide. I don't know how to code this. It would be nice if another feasible approach can be notified of or someone can point me in the right direction of how to code this.

Find circles that intersects a point [duplicate]

This question already has an answer here:
How to define a circle for a mongo db schema?
(1 answer)
Closed 6 years ago.
I'm getting in trouble when I try to run this code on mongodb
var partners = db.partners.find({})
var kmToRadius = function(km){
var earthRadiusInKm = 6378.1;
return km / earthRadiusInKm;
}
db.runCommand({
$centerSphere: [ [partners.loc], kmToRadius(partners.km) ] :{
$geoIntersects:{
$geometry: { type: "Point", coordinates: [ -73.93414657, 40.82302903 ] }
}
}
})
What I'm trying to do is get all the partners location (which are in geojson format), make a circle using $centerSphere and verify if there is intersection with a coordinate.
I know I can't store circles in GeoJson format, only polygons, which turns very difficult to do what I want. Someone knows if there is another way to make this work ? Thanks
My partners collection seems like this:
{
"_id" : ObjectId("583315cfa9d41218cc9c833f"),
"updatedAt" : ISODate("2016-11-21T15:42:07.703Z"),
"createdAt" : ISODate("2016-11-21T15:42:07.703Z"),
"name" : "partnerName",
"mainEmail" : "email",
"password" : "$2a$10$WJC6WzZNM8NyDKQgovJa.OICLOMV6Qp6xcGLE3fRcUGuBa8Zhy8qy",
"km" : 10,
"loc" : {
"type" : "Point",
"coordinates" : [
-46.62217,
-23.668224
]
},
"rate" : 4,
"nServices" : 35
}
But what I want to do is create a radius for each partner and check which partners intersects a point, instead of get the partners within a radius.

Getting counts of embedded collections in a MongoDB Document

I am using MongoDB and the 10Gen node.js driver.
I have a collection in MongoDB that has docs similar to this:
{
_id: ObjectId( "500310affdc47af710000001" ),
name:'something',
tags:['apple','fruit','red'],
created: Date( 1342378221351 )
}
What I would like to get is look at all the documents and get a distinct count of all tags across all documents in the collection. I tried the group function and got this:
> db.photos.group(
... {cond:{}
... ,key:{tags:true}
... ,initial:{count:0}
... ,reduce:function(doc,out){out.count++}
... ,finalize:function(out){}});
[
{
"tags" : null,
"count" : 35
},
{
"tags" : [
"#strawberry",
"#friutpicture"
],
"count" : 1
}
]
Which is not right. I can get the distinct without the counts like this:
> db.photos.distinct('tags')
[
"#friutpicture",
"#strawberry",
"#factory",
"#wallpaper",
"#bummber",
"#test",
"#fire",
"#watermark"
]
but I need the counts.
You can use the following in the new Aggregation Framework (2.1+):
> db.photos..aggregate([{$unwind:"$tags"},
{$group:{_id:"$tags"}},
{$group:{_id:"DistictCount",count:{$sum:1}}}])
Your result will be:
{
"result" : [
{
"_id" : "DistictCount",
"count" : 8
}
],
"ok" : 1
}
The group function won't do what you need because you need to "unroll" the tag array before you can group on it and that means you need a map function that emits each tag in a document, as well as a reduce. You can use map/reduce if you are stuck on 2.0 or earlier and can't use aggregation framework. Here is an example that may help.

Resources