Storing data to array from JSON response - axios

I am calling google Places API which returns a JSON response similar to this, from this responss I would like to extract all the name objects and store them in an array:
{
"html_attributions" : [],
"next_page_token" : "CpQCAgEAAFxg8o-eU7_uKn7Yqjana-",
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.867217,
"lng" : 151.195939
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
"id" : "7eaf747a3f6dc078868cd65efc8d3bc62fff77d7",
"name" : "Biaggio Cafe - Pyrmont",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 600,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAmWmj0BqA0Jorm1_vjAvx1n6c7ZNBxyY-U9x99-oNyIBE",
"width" : 900
}
],
"place_id" : "ChIJIfBAsjeuEmsRdgu9Pl1Ps48",
"price_level" : 1,
"types" : [ "cafe", "bar", "restaurant", "food", "establishment" ],
"vicinity" : "48 Pirrama Rd, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.866786,
"lng" : 151.195633
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3ef986cd56bb3408bc1cf394f3dad9657c1d30f6",
"name" : "Doltone House",
"photos" : [
{
"height" : 1260,
"html_attributions" : [ "From a Google User" ],
"photo_reference" : "CnRwAAAAeM-ag",
"width" : 1890
}
],
"place_id" : "ChIJ5xQ7szeuEmsRs6Kj7YFZE9k",
"reference" : "CnRvAAAA22k1PAGyDxAgHZk6ErHh_h_mLUK_8XNFLvixPJHXRbCzg-",
"types" : [ "food", "establishment" ],
"vicinity" : "48 Pirrama Rd, Pyrmont"
},
{
"aspects" : [
{
"rating" : 23,
"type" : "overall"
}
],
...
],
"status" : "OK"
}
In my code, I would like to store all instances of name in an array. I'm not really sure how to do this, but what I'm doing right now is definitely not working, the array remains empty after console logging it.
var place_search = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" + encodeURIComponent(agent.parameters.cuisine) + "restaurant" + "&region=sg&location=" + lat + "," + long + "&radius=" + user_proximity + "&opennow" + "&key=" + api_key;
return axios.get(place_search)
.then(response => {
const { place_id, name, formatted_address } = response.data.results;
placeArray.push({
name: name,
place_id: place_id
});
)}

There are a number of issues with your code, and it isn't clear what you think each part is trying to do, but the biggest issue seems to be with how you're handling the call with axios inside the loop. In particular, there seem to be two issues:
You're returning the value from the call - this breaks out of the loop after executing the then portion the first time.
Related to this, the code after the return can't get executed (you've returned from the function), so you never evaluate if the rating is greater than 4.
Assuming you're trying to loop through the places in placeArray and make a network call with axios for each one, you may want to use await, which is available in more modern versions of node. It might look something like this:
for (var i = 0; i < placeArray.length; i++) {
var place_details = "https://maps.googleapis.com/maps/api/place/details/json?place_id=" + placeArray[i].place_id + "&fields=rating&key=" + api_key;
const response = await axios.get(place_details);
const ratings = response.data.result.rating;
if (ratings > 4 ){
agent.add(`Ok I recommend ` + JSON.stringify(placeArray[i].name) + ` and the address is ` + JSON.stringify(placeArray[i].formatted_address));
agent.add(`It is rated ` + placeArray[i].ratings )
break;
}
}

Related

MongoDB delete specific nested element in object array

I want to remove this
"lightControl" : 75
from this
{
"_id" : "dfdfwef-fdfd-fd94284o-aafg",
"name" : "Testing",
"serialNumber" : "fhidfhd8ghfd",
"description" : "",
"point" : {
"type" : "Point",
"coordinates" : [
10.875447277532754,
20.940549069378634
]
},
"ancestors" : [ ],
"metadata" : {
"measurement" : {
"high" : "40000.0",
"medium" : "25000.0"
},
"digitalTwin" : {
},
"emails" : [
""
],
"lastMeasurementDate" : "2010-03-04T11:32:06.691Z",
"lightControl" : 75
},
"tags" : [ ],
"createdAt" : ISODate("2019-12-07T15:22:10.988Z"),
"updatedAt" : ISODate("2020-03-08T15:38:21.736Z"),
"_class" : "com.test.demo.api.model.Device"
}
All I want is for this specific id, to completely delete the lightControl element from metadata. I have tried $pull, but I am probably missing something. Any ideas? Thank you in advance!
Your lightControl is not in array, for nested property, use the dot wrapped in doublequotes:
MongoDB shell:
db.getCollection('yourCollectionName').update({},{$unset:{
"metadata.lightControl":""
}})
In case you have a list of objects with _id(s) instead of updating directly, assume Node.js client for MongoDB is used:
// import mongodb from "mongodb"; // ES6
const mongodb = require("mongodb");
var client = MongoClient(...);
var coll = client["yourDbName"]["yourCollectionName"];
var objs = []; // <-- The array with _id(s)
for (let i=0; i<objs.length; i++){
let id = mongodb.ObjectID(objs[i]["_id"]);
coll.update({ _id:id },{$unset:{ "metadata.lightControl":"" }});
}

Python check MongoDB field exists or not

I have two collections one is websites which stores information like:
{
"_id" : ObjectId("5ac5efd6a37efa4c0e28f5aa"),
"main_id" : 3,
"status" : "",
"website" : "http://test.com",
"last_access_time" : "2018-04-16 17:49:03",
"links" : [
{
"link_id" : "test-1",
"link" : "test1.html"
},
{
"link_id" : "test-2",
"link" : "test.html"
}
]
}
And another is website_info in which I want store info like:
{
"_id" : ObjectId("5ad72ddecf45b60dffcbf9f2"),
"main_id" : 3,
"last_access_time" : "2018-04-18 15:37:02",
"test-1" : {
"no_of_links" : 55,
"links_2" : [
{
"link" : "/home",
"link_id" : "secnd-1",
},
{
"link" : "/login",
"link_id" : "secnd-2",
},
{
"link" : "/services",
"link_id" : "secnd-3",
}
]
},
"test-2" : {
"no_of_links" : 55,
"links_2" : [
{
"link" : "/home",
"link_id" : "secnd-1",
},
{
"link" : "/login",
"link_id" : "secnd-2",
},
{
"link" : "/services",
"link_id" : "secnd-3",
}
]
}
}
I am using Python3 and mongoDB.
Here I want to check the field like "link_id" which is "test-1" in the website_info for main_id = 3 exists or not. If it is exists I will update for same, if does not exists I want to insert new record set.
The thing is how to check whether field "test-1" (which is the value from websites collection) in website_info collection exists or not.
Help is appreciated.
Here in my case, link_id will be unique in website_info collection. So no need to check for main_id, only checking for link_id solved my issue, like:
#classmethod
def find_link(self, link_id):
cursor = self.db.collection.find({link_id: {'$exists': True}} )
results = list(cursor)
return results
And check for exists like:
if(len(is_exists)>0):
#do if exists

Why does MapReduce return undefined for a field that exists in document?

I am trying to debug a strange issue I am running into while running mapreduce on a collection:
For reference, here's a single document from the collection:
{
"_id" : "ITOUXFWgvWs",
"source" : "youtube",
"insert_datetime" : ISODate("2017-04-06T22:27:43.598Z"),
"processed" : false,
"raw" : {
"id" : "ITOUXFWgvWs",
"etag" : "\"m2yskBQFythfE4irbTIeOgYYfBU/hiQtS6aptLlqxTpsYp1EJIRcoZo\"",
"snippet" : {
"publishedAt" : ISODate("2017-04-06T13:25:28Z"),
"title" : "Alarm.com: The Only Smart Home App You Need",
"channelId" : "UC_HZfoZUP36STk7SrtKYH4g",
"description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home? It can all come together with Alarm.com.",
"categoryId" : "28",
"channelTitle" : "Alarm.com",
"thumbnails" : {
"default" : {
"height" : 90,
"width" : 120,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/default.jpg"
},
"standard" : {
"height" : 480,
"width" : 640,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/sddefault.jpg"
},
"high" : {
"height" : 360,
"width" : 480,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/hqdefault.jpg"
},
"medium" : {
"height" : 180,
"width" : 320,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/mqdefault.jpg"
},
"maxres" : {
"height" : 720,
"width" : 1280,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/maxresdefault.jpg"
}
},
"liveBroadcastContent" : "none",
"localized" : {
"title" : "Alarm.com: The Only Smart Home App You Need",
"description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home? It can all come together with Alarm.com."
}
},
"contentDetails" : {
"duration" : "PT37S",
"dimension" : "2d",
"definition" : "hd",
"licensedContent" : false,
"projection" : "rectangular",
"caption" : "false"
},
"kind" : "youtube#video",
"statistics" : {
"likeCount" : "0",
"dislikeCount" : "0",
"favoriteCount" : "0",
"viewCount" : "32"
},
"uploaded" : ISODate("2017-04-06T13:25:28Z")
},
}
I am literally following the mapreduce debug steps from official mongo documentation.
Here's what my mapreduce script looks like:
var map = function() {
emit("1", this._id);
};
var emit = function(key, value) {
print("emit");
print("key: " + key + " value: " + tojson(value));
}
var myDoc = db.getCollection("abc").find({}).limit(1);
map.apply(myDoc);
And it always produces the result like this:
MongoDB shell version: 2.4.6
connecting to: test
emit
key: 1 value: undefined
I expect the script to emit the _id since it clearly exists in the document but it doesn't.
What might be possible cause of this?
find() always returns a cursor.
Replace it with findOne()
var myDoc = db.getCollection("abc").findOne({});
Or store the documents in an Array using toArray()
var myDoc = db.getCollection("abc").find({}).limit(1).toArray()[0];

MongoDB Database

well... I don't know anything about not relational data base and I wanna know if the following script is correct.
The data base it's developed for obtain management of users and schedules, (The schedules are related with a professor and a classroom). The classrooms have a code and they can be commented and rated by users.
db.user.insert({
//Can be 6 o 10 digits
_id : 201309,
name : {
First : "Fernando",
Last : "Sarmiento"
},
area : 2,
account : {
email : "email#google.com",
password : "Password1!",
level : 0
},
status : {
status : "Active",
Date : 17-01-2016
},
//Can be null
comment : "I like cereal",
})
-
db.schedule.insert({
_id: ObjectId(),
day : "Lunes",
hour : 13 : 00,
finalHour : 14 : 00,
teacher : {
$ref : "user",
$id : $id.user
},
classroom : {
$ref : "classroom",
$id : $id.classrom
}
})
-
db.classroom.insert({
_id : ObjectId(),
name : "Software Development",
comment : "classrom 3",
//Can take values from 0 to 5
score : $avg(userScore),
code : [
{
code : "ABCDEF123456",
user : [{
$ref : "user",
$id : $id.user
}]
}
],
post : [
{
post : "I like the classrom :)",
date : 17-01-2016,
user : [{
$ref : "user",
$id : $id.user
}]
}
],
userScore : [{
score : 4.5,
user : [{
$ref : "user",
$id : $id.user
}]
}]
})
Thanks! :3

MongoDB - how to query for a nested item inside a collection?

I have some data that looks like this:
[
{
"_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
"advertisers" : [
{
"created_at" : ISODate("2011-07-26T21:02:19Z"),
"category" : "Infinity Pro Spin Air Brush",
"updated_at" : ISODate("2011-07-26T21:02:19Z"),
"lowered_name" : "conair",
"twitter_name" : "",
"facebook_page_url" : "",
"website_url" : "",
"user_ids" : [ ],
"blog_url" : "",
},
and I was thinking that a query like this would give the id of the advertiser:
var start = new Date(2011, 1, 1);
> var end = new Date(2011, 12, 12);
> db.agencies.find( { "created_at" : {$gte : start , $lt : end} } , { _id : 1 , program_ids : 1 , advertisers { name : 1 } } ).limit(1).toArray();
But my query didn't work. Any idea how I can add the fields inside the nested elements to my list of fields I want to get?
Thanks!
Use dot notation (e.g. advertisers.name) to query and retrieve fields from nested objects:
db.agencies.find({
"advertisers.created_at": {
$gte: start,
$lt: end
}
},
{
_id: 1,
program_ids: 1,
"advertisers.name": 1
}
}).limit(1).toArray();
Reference: Retrieving a Subset of Fields
and Dot Notation
db.agencies.find(
{ "advertisers.created_at" : {$gte : start , $lt : end} } ,
{ program_ids : 1 , advertisers.name : 1 }
).limit(1).pretty();
There is one thing called dot notation that MongoDB provides that allows you to look inside arrays of elements. Using it is as simple as adding a dot for each array you want to enter.
In your case
"_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
"advertisers" : [
{
"created_at" : ISODate("2011-07-26T21:02:19Z"),
"category" : "Infinity Pro Spin Air Brush",
"updated_at" : ISODate("2011-07-26T21:02:19Z"),
"lowered_name" : "conair",
"twitter_name" : "",
"facebook_page_url" : "",
"website_url" : "",
"user_ids" : [ ],
"blog_url" : "",
},
{ ... }
If you want to go inside the array of advertisers to look for the property created_at inside each one of them, you can simply write the query with the property {'advertisers.created_at': query} like follows
db.agencies.find( { 'advertisers.created_at' : { {$gte : start , $lt : end} ... }