How to get only latest result from /page_fans_country? - facebook

I am using this query in facebook api
nikhilglass/insights/page_fans_country?
But it gives me an array of results
data": [
{
"name": "page_fans_country",
"period": "lifetime",
"values": [
{
"value": {
"BD": 1,
"IN": 136,
"SA": 1,
"SY": 1
},
"end_time": "2017-02-06T08:00:00+0000"
},
{
"value": {
"BD": 1,
"IN": 136,
"SA": 1,
"SY": 1
},
"end_time": "2017-02-07T08:00:00+0000"
},
{
"value": {
"BD": 1,
"IN": 136,
"SA": 1,
"SY": 1
..
....
pagination:next_result
But i just want to get the likes for current time , i don't need the previous count, so end_time should be current date and time . is their any parameter for query so that i an get only one result(latest)
help please

response["data"][0]["values"][2]["value"]

Related

Filter results in mongoDb based on status

I have an array like this
[
{
"name": "CAMP-1",
"status": "incomplete",
"version": 3,
},
{
"name": "CAMP-1",
"status": "complete",
"version": 2,
},
{
"name": "CAMP-1",
"status": "complete",
"version": 1,
},
{
"name": "CAMP-2",
"status": "complete",
"version": 2,
},
{
"name": "CAMP-2",
"status": "incomplete",
"version": 1,
}
]
if the status of latest version is incomplete then both the latest incomplete and complete versions should be returned.
if the status of the latest version is complete then only that version should be returned.
I tried to group by name and status which gives the latest version of incomplete and complete object
db.collection.aggregate({
"$sort": {
"version": -1
}
},
{
"$group": {
"_id": {
"content": "$name",
"status": "$status"
},
"status": {
"$first": "$$ROOT"
},
"content": {
"$first": "$$ROOT"
}
}
},
{
"$replaceRoot": {
"newRoot": "$content"
}
})
The output which I get is
[
{
"name": "CAMP-1",
"status": "incomplete",
"version": 3,
},
{
"name": "CAMP-1",
"status": "complete",
"version": 2,
},
{
"name": "CAMP-1",
"status": "complete",
"version": 1,
},
{
"name": "CAMP-2",
"status": "incomplete",
"version": 2,
}
]
But the expected output is
[
{
"name": "CAMP-1",
"status": "incomplete",
"version": 3,
},
{
"name": "CAMP-1",
"status": "complete",
"version": 2,
},
{
"name": "CAMP-2",
"status": "complete",
"version": 2,
}
]
Can anyone please help on how to filter the data based on status?
Query
group by name
find the max-complete version
filter to keep the completed with the same version and the uncompleted with bigger version
unwind and replace root
*For example for 1 name, if you have incomplete version 5 6 7 3 and complete 2 3 4 , you will get 5 6 7 incompletes and 4 complete.
If its not what you want exactly maybe with small changes you can get what you need.
Playmongo (to see what each stage does put the mouse on its end)
aggregate(
[{"$group": {"_id": "$name", "docs": {"$push": "$$ROOT"}}},
{"$set":
{"max-complete":
{"$let":
{"vars":
{"c":
{"$filter":
{"input": "$docs",
"cond": {"$eq": ["$$this.status", "complete"]}}}},
"in": {"$max": "$$c.version"}}}}},
{"$set":
{"docs":
{"$filter":
{"input": "$docs",
"cond":
{"$or":
[{"$and":
[{"$gt": ["$$this.version", "$max-complete"]},
{"$eq": ["$$this.status", "incomplete"]}]},
{"$and":
[{"$eq": ["$$this.version", "$max-complete"]},
{"$eq": ["$$this.status", "complete"]}]}]}}}}},
{"$unwind": "$docs"},
{"$replaceRoot": {"newRoot": "$docs"}}])

Increase Precision of Text Parsed via Facebook Duckling?

I'm using Facebook Duckling to parse some text but the results include some odd dimensions:
String text = "Tomorrow, February 28";
String result = Duckling.parseText(text);
Result:
[
{
"body": "Tomorrow, February 28",
"start": 0,
"value": {
"values": [
{
"value": "2022-02-28T00:00:00.000-08:00",
"grain": "day",
"type": "value"
}
],
"value": "2022-02-28T00:00:00.000-08:00",
"grain": "day",
"type": "value"
},
"end": 21,
"dim": "time",
"latent": false
},
{
"body": "28'",
"start": 19,
"value": {
"value": 28,
"type": "value",
"minute": 28,
"unit": "minute",
"normalized": {
"value": 1680,
"unit": "second"
}
},
"end": 22,
"dim": "duration",
"latent": false
},
{
"body": "28'",
"start": 19,
"value": {
"value": 28,
"type": "value",
"unit": "foot"
},
"end": 22,
"dim": "distance",
"latent": false
}
]
This result is odd since from the context of the query the text "28" is clearly referring to the day of month but Duckling also returns data as if it were referring to the Distance dimension.
Is there a way to make Duckling context aware and have it only return results matching the full query? Passing "dimensions" as argument is not ideal since I don't know the dimensions in advance.
Thanks

MongoDB - Way to find closest results from arrays inside every document

I want to find closest result from array inside document for every doc, and project it new object using MongoDB. It will be easier to explain what I trying to do by example:
Doc schema:
{
"id": "string",
"name": "string",
"track" : [
{
"time": "number",
"distance": "number"
}
]
}
EXAMPLE:
I want to find closest results for every doc for time equals 4
Input data:
[
{
"id": "1",
"name": "test1",
"track" : [
{
"time": 0,
"distance": 0
},
{
"time": 1,
"distance": 5
},
{
"time": 3,
"distance": 17
},
{
"time": 4,
"distance": 23
},
{
"time": 6,
"distance": 33
}
]
},
{
"id": "2",
"name": "test2",
"track" : [
{
"time": 0,
"distance": 0
},
{
"time": 1,
"distance": 5
},
{
"time": 2,
"distance": 12
},
{
"time": 4,
"distance": 26
},
{
"time": 6,
"distance": 32
}
]
},
{
"id": "3",
"name": "test3",
"track" : [
{
"time": 0,
"distance": 0
},
{
"time": 1,
"distance": 5
},
{
"time": 3,
"distance": 12
}
]
}
]
Output data:
[
{
"id": "1",
"result" : {
"time": 4,
"distance": 23
}
},
{
"id": "2",
"result" : {
"time": 4,
"distance": 26
}
},
{
"id": "3",
"result" : {
"time": 3,
"distance": 12
}
}
]
Is it possible to do this using MongoDB?
db.collection.aggregate([
{
"$addFields": {
"tracks": {
"$filter": {
"input": "$track",
"as": "track",
"cond": {
"$lte": [
"$$track.time",
4
]
}
}
}
}
},
{
"$addFields": {
"tracks": {
"$slice": [
"$tracks",
-1
]
}
}
},
{
"$unwind": "$tracks"
},
{
"$project": {
"tracks": 1,
"name": 1
}
}
])
Play
It does below things:
Finds whose track time is <=4 and adds it to an array called items
Then it gets the last element - i.e closer element
Take the element from array - unwind
Projects what is needed.

MongoDB Aggregation Error Returning wrong result

I have my json object like this
{
"_id": "5c2e811154855c0012308f00",
"__pclass": "QXRzXFByb2plY3RcTW9kZWxcUHJvamVjdA==",
"id": 44328,
"name": "Test project via postman2//2",
"address": "some random address",
"area": null,
"bidDate": null,
"building": {
"name": "Health Care Facilities",
"type": "Dental Clinic"
},
"collaborators": [],
"createdBy": {
"user": {
"id": 7662036,
"name": "Someone Here"
},
"firm": {
"id": 2520967,
"type": "ATS"
}
},
"createdDate": "2019-01-03T21:39:29Z",
"customers": [],
"doneBy": null,
"file": null,
"firm": {
"id": 1,
"name": "MyFirm"
},
"leadSource": {
"name": "dontknow",
"number": "93794497"
},
"location": {
"id": null,
"city": {
"id": 567,
"name": "Bahamas"
},
"country": {
"id": 38,
"name": "Canada"
},
"province": {
"id": 7,
"name": "British Columbia"
}
},
"modifiedBy": null,
"modifiedDate": null,
"projectPhase": {
"id": 1,
"name": "pre-design"
},
"quotes": [{
"id": 19,
"opportunityValues": {
"Key1": 100,
"Key2 Key2": 100,
"Key3 Key3 Key3": 200,
}
}],
"specForecast": [],
"specIds": [],
"tags": [],
"valuation": "something"
}
I am trying to aggregate using this query in MongoDB. My aggregation key is 4 level deep and also contains spaces. On all online examples shows me the aggregation at the first level. Looking to the online codes, I tried to re-iterate the same with my 4th level deep key.
db.mydata.aggregate([
{$match: {"id": 44328 } } ,
{$group: { _id: "$quotes.id",
totalKey2:{ $sum: "$quotes.opportunityValues.Key2 Key2"},
totalKey3:{ $sum: "$quotes.opportunityValues.Key3 Key3 Key3"}
}
}
]);
This should return
_id totalKey2 totalKey3
0 19 100 300
But it is returning
_id totalKey2 totalKey3
0 19 0 0
What am I doing Wrong?
Although it's not recommended to use space in field names in Mongo, it works as expected.
The problem with your query is that "quotes" is an array and you should first unwind it before grouping it.
This works as expected:
db.mydata.aggregate([
{ $match: { "id": 44328 } } ,
{ $unwind: "$quotes" },
{ $group: { _id: "$quotes.id",
totalKey2:{ $sum: "$quotes.opportunityValues.Key2 Key2" },
totalKey3:{ $sum: "$quotes.opportunityValues.Key3 Key3 Key3" } }
}
]);

Facebook Data Concern

i have concern about the data returned from facebook api for reach, below is the JSON data i am gonna post:
{
"id": "0123456789/insights/page_impressions_unique/day",
"name": "page_impressions_unique",
"period": "day",
"values": [
{
"value": 54,
"end_time": "2014-08-16T07:00:00+0000"
},
{
"value": 6,
"end_time": "2014-08-17T07:00:00+0000"
},
{
"value": 2,
"end_time": "2014-08-18T07:00:00+0000"
}
],
"title": "Daily Total Reach",
"description": "Daily: The number of people who have seen any content associated with your Page. (Unique Users)"
},
{
"id": "0123456789/insights/page_impressions_by_age_gender_unique/day",
"name": "page_impressions_by_age_gender_unique",
"period": "day",
"values": [
{
"value": {
"F.35-44": 20,
"M.35-44": 11,
"F.25-34": 10,
"M.25-34": 5,
"F.55-64": 4,
"F.45-54": 4,
"F.18-24": 2,
"M.13-17": 1,
"M.65+": 1,
"M.45-54": 1,
"M.18-24": 1
},
"end_time": "2014-08-16T07:00:00+0000"
},
{
"value": [
],
"end_time": "2014-08-17T07:00:00+0000"
},
{
"value": [
],
"end_time": "2014-08-18T07:00:00+0000"
}
],
"title": "Daily Reach Demographics",
"description": "Daily: Total Page Reach by age and gender. (Unique Users)"
}
looking above data i get concerned,
the data for 16th for unique page reach is 54, and in demographic detail data the reach becomes on summing 60 for 16th,
similarly data returned from facebook for 17th and 18th are totally different.
why is it so? what should i do to get the proper data?