Related
I need to make a query where it matches a specific value of an array. Not all the values of the array but only one. I know in mongo there is a $in operator so you can match 1 value of the array.
I have written my code something like this:
const getSearch = async (req,res) => {
try{
//query = { establishment: { name: "foodtruck" }, configuration: { name: "lineal" } }
console.log(query)
let doesExist = await Kitchen.find({ "establishment": { "name": { "$in": ["foodtruck","salon"] } }})
console.log(doesExist)
}catch(err){
throw err
}
}
In my local mongo i have different objects that matches the establishment name. Here they are:
db.kitchens.find()
{ "_id" : ObjectId("6084ca4c49ddab30473a7396"), "anafes" : { "quantity" : "1" }, "norms" : [ "norm5", "norm4" ], "availableDays" : [ "4/26/2021", "4/27/2021", "4/28/2021", "4/29/2021", "4/30/2021", "5/1/2021", "5/10/2021", "5/11/2021", "5/12/2021", "5/13/2021", "5/14/2021", "5/15/2021", "5/16/2021", "5/17/2021", "5/18/2021", "5/19/2021", "5/2/2021", "5/20/2021", "5/21/2021", "5/22/2021", "5/3/2021", "5/4/2021", "5/5/2021", "5/6/2021", "5/7/2021", "5/8/2021", "5/9/2021" ], "created_at" : ISODate("2021-04-25T01:26:29.542Z"), "updated_at" : ISODate("2021-04-25T01:26:29.542Z"), "deleted_at" : null, "establishment" : { "name" : "delivery" }, "dimensions" : { "name" : "40" }, "configuration" : { "name" : "lineal" }, "pictures" : [ { "_id" : ObjectId("6084ca4c49ddab30473a7397"), "url" : "https://hood-bucket-2021.s3.sa-east-1.amazonaws.com/images/1619315274721.jpg" } ], "timePrices" : [ { "_id" : ObjectId("6084ca4c49ddab30473a7398"), "time" : "perWeek", "price" : "5000" } ], "check_in" : { "from" : "00:00", "to" : "00:00" }, "check_out" : { "from" : "00:00", "to" : "00:00" }, "ovens" : { "name" : "convection", "brand" : "adidas" }, "location" : { "city" : "Buenos Aires", "province" : "Buenos Aires", "street" : "Teodoro Garcia", "number" : "1930", "coords" : [ { "_id" : ObjectId("6084ca4c49ddab30473a7399"), "lat" : "-34.5650491", "lng" : "-58.4415115" } ], "floor" : "9", "postal" : 1426 }, "type_of_time" : [ { "_id" : ObjectId("6084ca4c49ddab30473a739a"), "time" : "perWeek" } ], "host" : ObjectId("605f4e36d8281726d9be3ad3"), "__v" : 0 }
{ "_id" : ObjectId("6089f8a7bda5898d50876791"), "anafes" : { "quantity" : "1" }, "norms" : [ "norm2", "norm3", "norm8" ], "availableDays" : [ "4/30/2021", "5/1/2021", "5/10/2021", "5/11/2021", "5/12/2021", "5/13/2021", "5/14/2021", "5/15/2021", "5/16/2021", "5/17/2021", "5/18/2021", "5/19/2021", "5/2/2021", "5/20/2021", "5/21/2021", "5/22/2021", "5/23/2021", "5/24/2021", "5/25/2021", "5/26/2021", "5/27/2021", "5/28/2021", "5/29/2021", "5/3/2021", "5/30/2021", "5/31/2021", "5/4/2021", "5/5/2021", "5/6/2021", "5/7/2021", "5/8/2021", "5/9/2021", "7/1/2021", "7/10/2021", "7/11/2021", "7/12/2021", "7/13/2021", "7/14/2021", "7/15/2021", "7/16/2021", "7/17/2021", "7/18/2021", "7/19/2021", "7/2/2021", "7/20/2021", "7/21/2021", "7/22/2021", "7/23/2021", "7/24/2021", "7/25/2021", "7/26/2021", "7/27/2021", "7/28/2021", "7/29/2021", "7/3/2021", "7/30/2021", "7/31/2021", "7/4/2021", "7/5/2021", "7/6/2021", "7/7/2021", "7/8/2021", "7/9/2021" ], "created_at" : ISODate("2021-04-28T23:56:46.101Z"), "updated_at" : ISODate("2021-04-28T23:56:46.102Z"), "deleted_at" : null, "establishment" : { "name" : "foodtruck" }, "dimensions" : { "name" : "50" }, "configuration" : { "name" : "general" }, "pictures" : [ { "_id" : ObjectId("6089f8a7bda5898d50876792"), "url" : "https://hood-bucket-2021.s3.sa-east-1.amazonaws.com/images/1619654809186.jpg" }, { "_id" : ObjectId("6089f8a7bda5898d50876793"), "url" : "https://hood-bucket-2021.s3.sa-east-1.amazonaws.com/images/1619654809259.jpg" } ], "timePrices" : [ { "_id" : ObjectId("6089f8a7bda5898d50876794"), "time" : "perDay", "price" : "10000" } ], "check_in" : { "from" : "10:00", "to" : "12:00" }, "check_out" : { "from" : "15:00", "to" : "17:00" }, "ovens" : { "name" : "static", "brand" : "adidas" }, "location" : { "city" : "buenos aires", "coords" : [ { "_id" : ObjectId("6089f8a7bda5898d50876795"), "lat" : "-34.5659098", "lng" : "-58.43880350000001" } ], "province" : "buenos aires", "street" : "Olleros", "number" : "1870", "floor" : "4", "postal" : 1426 }, "type_of_time" : [ { "_id" : ObjectId("6089f8a7bda5898d50876796"), "time" : "perDay" } ], "host" : ObjectId("605f4e36d8281726d9be3ad3"), "__v" : 0 }
{ "_id" : ObjectId("608a09e29eda52a6cdc7d663"), "anafes" : { "quantity" : "1" }, "norms" : [ "norm1", "norm2", "norm4" ], "availableDays" : [ "4/30/2021", "6/1/2021", "6/10/2021", "6/11/2021", "6/12/2021", "6/13/2021", "6/14/2021", "6/15/2021", "6/16/2021", "6/17/2021", "6/18/2021", "6/19/2021", "6/2/2021", "6/20/2021", "6/21/2021", "6/22/2021", "6/23/2021", "6/24/2021", "6/25/2021", "6/26/2021", "6/27/2021", "6/28/2021", "6/29/2021", "6/3/2021", "6/30/2021", "6/4/2021", "6/5/2021", "6/6/2021", "6/7/2021", "6/8/2021", "6/9/2021" ], "created_at" : ISODate("2021-04-29T00:47:45.376Z"), "updated_at" : ISODate("2021-04-29T00:47:45.376Z"), "deleted_at" : null, "establishment" : { "name" : "foodtruck" }, "dimensions" : { "name" : "50" }, "configuration" : { "name" : "lineal" }, "pictures" : [ { "_id" : ObjectId("608a09e29eda52a6cdc7d664"), "url" : "https://hood-bucket-2021.s3.sa-east-1.amazonaws.com/images/1619659217736.jpg" }, { "_id" : ObjectId("608a09e29eda52a6cdc7d665"), "url" : "https://hood-bucket-2021.s3.sa-east-1.amazonaws.com/images/1619659217747.jpg" }, { "_id" : ObjectId("608a09e29eda52a6cdc7d666"), "url" : "https://hood-bucket-2021.s3.sa-east-1.amazonaws.com/images/1619659217668.jpg" } ], "timePrices" : [ { "_id" : ObjectId("608a09e29eda52a6cdc7d667"), "time" : "perDay", "price" : "5000" }, { "_id" : ObjectId("608a09e29eda52a6cdc7d668"), "time" : "perWeek", "price" : "15000" } ], "check_in" : { "from" : "07:00", "to" : "08:00" }, "check_out" : { "from" : "14:00", "to" : "20:00" }, "ovens" : { "name" : "convection", "brand" : "super", "model" : "adibas" }, "location" : { "city" : "BUenos aires", "coords" : [ { "_id" : ObjectId("608a09e29eda52a6cdc7d669"), "lat" : "-34.5793959", "lng" : "-58.40673389999999" } ], "province" : "Buenos aires", "street" : "Avenida del libertador", "number" : "2550", "floor" : "10", "postal" : 1426 }, "type_of_time" : [ { "_id" : ObjectId("608a09e29eda52a6cdc7d66a"), "time" : "perDay" }, { "_id" : ObjectId("608a09e29eda52a6cdc7d66b"), "time" : "perWeek" } ], "host" : ObjectId("605f4e36d8281726d9be3ad3"), "__v" : 0 }
But when I run this query:
let doesExist = await Kitchen.find({ "establishment": { "name": { "$in": ["foodtruck","salon"] } }})
it does not match anything, but if I run this query:
let doesExist = await Kitchen.find({ "establishment": { "name": "foodtruck" }})
It does match. I don't understand what I am doing wrong, or why the $in operator is not.
working.
Can someone help me?????????
let doesExist = await Kitchen.find({"establishment.name": {"$in": ["foodtruck","salon"]}})
I have data from mongodb like this from one collection.
/* 1 */
{
"_id" : ObjectId("5be94355f220b62c7449dc0f"),
"districts" : [
{
"name" : "NORTH AND MIDDLE",
"code" : 632.0
},
{
"name" : "EAST",
"code" : 603.0
},
{
"name" : "SOUTH",
"code" : 602.0
}
],
"state" : "ISLANDS"
}
/* 2 */
{
"_id" : ObjectId("5be94355f220b62c7441dc04"),
"districts" : [
{
"name" : "Apple",
"code" : 512.0
},
{
"name" : "Ball",
"code" : 522.0
}
],
"state" : "GOLD"
}
/* 3 */
{
"_id" : ObjectId("5eee07816a011d391a45178"),
"districts" : [
{
"name" : "DAM",
"code" : 478.0
},
{
"name" : "DEN",
"code" : 481.0
},
{
"name" : "DOG AND CAT",
"code" : 461.0
}
],
"state" : "THE NAGAR AND HAVELI"
}
I was given an excel sheet like below as shown with no other information only 2 columns
My work is to add "Short Name of District" for all districts.
I tried below method
var tc = [
"NORTH AND MIDDLE",
"EAST",
"SOUTH",
"Apple",
"Ball ",
"DAM ",
"DEN ",
"DOG AND CAT"
]
db.dummy.find({"districts.name":{$in:tc}}).forEach(x => {
x["districts"].forEach( y => {
if (
y.name == "NORTH AND MIDDLE" ){
y.short_name = "NAM"
}
if (
y.name == "EAST" ){
y.short_name = "ET"
}
if (
y.name == "SOUTH" ){
y.short_name = "ST"
}
})
})
I got the result
/* 1 */
{
"_id" : ObjectId("5be94355f220b62c7449dc0f"),
"districts" : [
{
"name" : "NORTH AND MIDDLE",
"code" : 632.0,
"short_name" : "NAM"
},
{
"name" : "EAST",
"code" : 603.0,
"short_name" : "ET"
},
{
"name" : "SOUTH",
"code" : 602.0,
"short_name" : "ST"
}
],
"state" : "ISLANDS"
}
/* 2 */
{
"_id" : ObjectId("5be94355f220b62c7441dc04"),
"districts" : [
{
"name" : "Apple",
"code" : 512.0,
"short_name" : "Al"
},
{
"name" : "Ball",
"code" : 522.0
"short_name" : "BA"
}
],
"state" : "GOLD"
}
/* 3 */
{
"_id" : ObjectId("5eee07816a011d391a45178"),
"districts" : [
{
"name" : "DAM",
"code" : 478.0,
"short_name" : "DA"
},
{
"name" : "DEN",
"code" : 481.0,
"short_name" : "DN"
},
{
"name" : "DOG AND CAT",
"code" : 461.0
"short_name" : "DAC"
}
],
"state" : "THE NAGAR AND HAVELI"
}
Is this is the only method ??
like using if loop for all districts or any other methods are there like using mongodb aggregate or any other javascript methods. It will be helpful if other methods are there as it will be problem to use if loop when there is 730 districts are there. I dont have experience in working with aggregate frameworks so i thought anyone might know other method.
You may write a mapping:
const districtNameToShort = {
'NORTH AND MIDDLE': 'NAM',
'EAST': 'ET',
...
}
Then in your forEach
const districtNameToShort = {
'NORTH AND MIDDLE': 'NAM',
'EAST': 'ET'
}
db.dummy.find().forEach(x => {
db.dummy.update(
{_id : x._id},
{$set: {
districts: x.districts.map(district => {
district.short_name = districtNameToShort[district.name] || district.name
return district
})
}}
)
})
I am using the below code for service now rest outbound call and this is working as expected, but in the second rest call i am trying to parse the JSON body but I am unable to retrieve the values in the required table format
try {
var r = new sn_ws.RESTMessageV2('test', 'post');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print(response.getBody());
gs.print(response.getStatusCode());
var JsonObject = JSON.parse(responseBody);
var sid = JsonObject.sid;
gs.print(sid);
var r1 = new sn_ws.RESTMessageV2('gateways', 'POST' );
r1.setRequestHeader("X-chkp-sid",sid );
var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus = response1.getStatusCode()
gs.print(response1.getBody());
var parser = new JSONParser();
var jsonObj = parser.parse(responseBody1);
var outputString = 'Name Domain-Name OS Cluster-Members UID';
for (var i=0; i < jsonObj.objects.length; i++) {
var obj = jsonObj.objects[i];
outputString = outputString + "\n" + obj.name + ", " + obj.domain.name + ", " + obj["operating-system"] + ", " + obj["cluster-member-names"] + ", " + obj.uid;
}
gs.log(outputString);
}
catch(ex) {
var message = ex.message;
}
The expected output should be like this:
Link to image
But I am getting and output like this:
*** Script: Name Domain-Name OS Cluster-Members UID
clus-cn-1, dom-cn-1, Gaia, dev-cn-c1,dev-cn-c2, 16b96771-d13a-4c11-b457-9c0861aaf3c8
clus-cn-2, dom-cn-2, Gaia, dev-cn-c3,dev-cn-c4, 385c0a22-275c-4a70-9489-2b6ccd191eb8
dev-cn-c1, dom-cn-1, undefined, undefined, 4652da03-0e2f-4a0a-880c-338396be0818
dev-cn-c2, dom-cn-1, undefined, undefined, 2dbbbfd4-80ac-4fd9-b792-0a5b468c6409
dev-cn-c3, dom-cn-2, undefined, undefined, 6037e235-f19a-49ac-a39c-4889d979acbe
dev-cn-c4, dom-cn-2, undefined, undefined, 022c44df-9271-46db-b782-da084c476dd2
dom_cn_1_Server, dom-cn-1, Unknown OS, undefined, 4f417c60-3541-4c0f-a542-9a100d857077
dom_cn_2_Server, dom-cn-2, Unknown OS, undefined, cebecd33-1efb-4530-9de9-7c666e588ee7
How can I avoid the values after first two lines
Sample json
{
"objects" : [ {
"uid" : "16b96771-d13a-4c11-b457-9c0861aaf3c8",
"name" : "clus-cn-1",
"type" : "CpmiGatewayCluster",
"domain" : {
"uid" : "16280a32-183a-4050-b698-f59dbe488da6",
"name" : "dom-cn-1",
"domain-type" : "domain"
},
"policy" : {
"access-policy-installed" : true,
"access-policy-name" : "Standard",
"access-policy-installation-date" : {
"posix" : 1533629226178,
"iso-8601" : "2018-08-07T16:07+0800"
},
"threat-policy-installed" : false
},
"operating-system" : "Gaia",
"hardware" : "Open server",
"version" : "R80",
"ipv4-address" : "10.126.102.12",
"network-security-blades" : {
"firewall" : true,
"site-to-site-vpn" : true
},
"management-blades" : { },
"cluster-member-names" : [ "dev-cn-c1", "dev-cn-c2" ],
"vpn-encryption-domain" : "addresses_behind_gw",
"sic-status" : "uninitialized",
"tags" : [ ],
"icon" : "NetworkObjects/cluster",
"groups" : [ ],
"comments" : "",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533629193330,
"iso-8601" : "2018-08-07T16:06+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533629186803,
"iso-8601" : "2018-08-07T16:06+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "385c0a22-275c-4a70-9489-2b6ccd191eb8",
"name" : "clus-cn-2",
"type" : "CpmiGatewayCluster",
"domain" : {
"uid" : "6c2f639f-db32-4255-88ef-fb9b0e820051",
"name" : "dom-cn-2",
"domain-type" : "domain"
},
"policy" : {
"access-policy-installed" : true,
"access-policy-name" : "Standard",
"access-policy-installation-date" : {
"posix" : 1533631724838,
"iso-8601" : "2018-08-07T16:48+0800"
},
"threat-policy-installed" : false
},
"operating-system" : "Gaia",
"hardware" : "Open server",
"version" : "R80",
"ipv4-address" : "10.126.102.16",
"network-security-blades" : {
"firewall" : true
},
"management-blades" : { },
"cluster-member-names" : [ "dev-cn-c3", "dev-cn-c4" ],
"vpn-encryption-domain" : "addresses_behind_gw",
"sic-status" : "uninitialized",
"tags" : [ ],
"icon" : "NetworkObjects/cluster",
"groups" : [ ],
"comments" : "",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533631690759,
"iso-8601" : "2018-08-07T16:48+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533630083711,
"iso-8601" : "2018-08-07T16:21+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "4652da03-0e2f-4a0a-880c-338396be0818",
"name" : "dev-cn-c1",
"type" : "CpmiClusterMember",
"domain" : {
"uid" : "16280a32-183a-4050-b698-f59dbe488da6",
"name" : "dom-cn-1",
"domain-type" : "domain"
},
"policy" : { },
"ipv4-address" : "10.126.102.9",
"interfaces" : [ {
"interface-name" : "eth1",
"ipv4-address" : "10.10.10.1",
"ipv4-network-mask" : "255.255.255.240",
"ipv4-mask-length" : 28,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : false,
"ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
"leads-to-dmz" : false
}
}, {
"interface-name" : "eth0",
"ipv4-address" : "10.126.102.9",
"ipv4-network-mask" : "255.255.255.192",
"ipv4-mask-length" : 26,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : true
}
} ],
"sic-status" : "communicating",
"tags" : [ ],
"icon" : "NetworkObjects/Cluster_member",
"groups" : [ ],
"comments" : "",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533629193630,
"iso-8601" : "2018-08-07T16:06+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533629187252,
"iso-8601" : "2018-08-07T16:06+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "2dbbbfd4-80ac-4fd9-b792-0a5b468c6409",
"name" : "dev-cn-c2",
"type" : "CpmiClusterMember",
"domain" : {
"uid" : "16280a32-183a-4050-b698-f59dbe488da6",
"name" : "dom-cn-1",
"domain-type" : "domain"
},
"policy" : { },
"ipv4-address" : "10.126.102.10",
"interfaces" : [ {
"interface-name" : "eth1",
"ipv4-address" : "10.10.10.2",
"ipv4-network-mask" : "255.255.255.240",
"ipv4-mask-length" : 28,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : false,
"ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
"leads-to-dmz" : false
}
}, {
"interface-name" : "eth0",
"ipv4-address" : "10.126.102.10",
"ipv4-network-mask" : "255.255.255.192",
"ipv4-mask-length" : 26,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : true
}
} ],
"sic-status" : "communicating",
"tags" : [ ],
"icon" : "NetworkObjects/Cluster_member",
"groups" : [ ],
"comments" : "",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533629193427,
"iso-8601" : "2018-08-07T16:06+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533629188258,
"iso-8601" : "2018-08-07T16:06+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "6037e235-f19a-49ac-a39c-4889d979acbe",
"name" : "dev-cn-c3",
"type" : "CpmiClusterMember",
"domain" : {
"uid" : "6c2f639f-db32-4255-88ef-fb9b0e820051",
"name" : "dom-cn-2",
"domain-type" : "domain"
},
"policy" : { },
"ipv4-address" : "10.126.102.13",
"interfaces" : [ {
"interface-name" : "eth0",
"ipv4-address" : "10.126.102.13",
"ipv4-network-mask" : "255.255.255.192",
"ipv4-mask-length" : 26,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : true
}
}, {
"interface-name" : "eth1",
"ipv4-address" : "10.10.20.1",
"ipv4-network-mask" : "255.255.255.240",
"ipv4-mask-length" : 28,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : false,
"ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
"leads-to-dmz" : false
}
} ],
"sic-status" : "communicating",
"tags" : [ ],
"icon" : "NetworkObjects/Cluster_member",
"groups" : [ ],
"comments" : "",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533631689704,
"iso-8601" : "2018-08-07T16:48+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533630084104,
"iso-8601" : "2018-08-07T16:21+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "022c44df-9271-46db-b782-da084c476dd2",
"name" : "dev-cn-c4",
"type" : "CpmiClusterMember",
"domain" : {
"uid" : "6c2f639f-db32-4255-88ef-fb9b0e820051",
"name" : "dom-cn-2",
"domain-type" : "domain"
},
"policy" : { },
"ipv4-address" : "10.126.102.14",
"interfaces" : [ {
"interface-name" : "eth0",
"ipv4-address" : "10.126.102.14",
"ipv4-network-mask" : "255.255.255.192",
"ipv4-mask-length" : 26,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : true
}
}, {
"interface-name" : "eth1",
"ipv4-address" : "10.10.20.2",
"ipv4-network-mask" : "255.255.255.240",
"ipv4-mask-length" : 28,
"dynamic-ip" : false,
"topology" : {
"leads-to-internet" : false,
"ip-address-behind-this-interface" : "network defined by the interface ip and net mask",
"leads-to-dmz" : false
}
} ],
"sic-status" : "communicating",
"tags" : [ ],
"icon" : "NetworkObjects/Cluster_member",
"groups" : [ ],
"comments" : "",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533631690171,
"iso-8601" : "2018-08-07T16:48+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533630085006,
"iso-8601" : "2018-08-07T16:21+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "4f417c60-3541-4c0f-a542-9a100d857077",
"name" : "dom_cn_1_Server",
"type" : "CpmiHostCkp",
"domain" : {
"uid" : "16280a32-183a-4050-b698-f59dbe488da6",
"name" : "dom-cn-1",
"domain-type" : "domain"
},
"policy" : { },
"operating-system" : "Unknown OS",
"hardware" : "Open server",
"version" : "R80.10",
"ipv4-address" : "10.126.102.11",
"network-security-blades" : { },
"management-blades" : {
"network-policy-management" : true,
"logging-and-status" : true
},
"sic-status" : "uninitialized",
"tags" : [ ],
"icon" : "NetworkObjects/CheckPoint/Hosts/xHost_CP",
"groups" : [ ],
"comments" : "Domain Management Server",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533621654552,
"iso-8601" : "2018-08-07T14:00+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533621625970,
"iso-8601" : "2018-08-07T14:00+0800"
},
"creator" : "System"
},
"read-only" : true
}, {
"uid" : "cebecd33-1efb-4530-9de9-7c666e588ee7",
"name" : "dom_cn_2_Server",
"type" : "CpmiHostCkp",
"domain" : {
"uid" : "6c2f639f-db32-4255-88ef-fb9b0e820051",
"name" : "dom-cn-2",
"domain-type" : "domain"
},
"policy" : { },
"operating-system" : "Unknown OS",
"hardware" : "Open server",
"version" : "R80.10",
"ipv4-address" : "10.126.102.15",
"network-security-blades" : { },
"management-blades" : {
"network-policy-management" : true,
"logging-and-status" : true
},
"sic-status" : "uninitialized",
"tags" : [ ],
"icon" : "NetworkObjects/CheckPoint/Hosts/xHost_CP",
"groups" : [ ],
"comments" : "Domain Management Server",
"color" : "black",
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1533627992781,
"iso-8601" : "2018-08-07T15:46+0800"
},
"last-modifier" : "System",
"creation-time" : {
"posix" : 1533627964438,
"iso-8601" : "2018-08-07T15:46+0800"
},
"creator" : "System"
},
"read-only" : true
} ],
"from" : 1,
"to" : 8,
"total" : 8
}
Well, it looks like the code is doing what was meant to do.
If you want to filter only the lines with OS name of Gaia, you could just change your for to:
for (var i=0; i < jsonObj.objects.length; i++) {
if (obj["operating-system"] == "Gaia") {
var obj = jsonObj.objects[i];
outputString = outputString + "\n" +
obj.name + ", " +
obj.domain.name + ", " +
obj["operating-system"] + ", " +
obj["cluster-member-names"] + ", " + obj.uid;
}
}
I'm able to do
db.getCollection('comps').find({"stats.cat3.detail.name" : "Friendlyness"})`
This shows the proper docs the have a name of "Friendlyness" in the detail array.
I wanted to change "Friendlyness" to "friendliness"
I tried :
db.getCollection('comps').update(
{"stats.cat3.detail.name" : "Friendlyness"},
{$set : {"stats.cat3.detail.name" : "Friendliness"} }
)
and got errors like
" cannot use the part (stats of stats.cat3.detail.name) to traverse the element ({stats: [ { cat3: ...."
I tried putting in position operators "$" all over the update object property. but still get errors.
sample data:
{
"_id" : ObjectId("58d94c441eb9e52454932db6"),
"desc" : "Manufacturer of a wide range of consumer electronics products including audio, video, communications, and computer systems.",
"source" : "alexa",
"category" : "Consumer Electronics",
"name" : "comp1.com",
"__v" : 0,
"stats" : [
{
"cat1" : {
"detail" : [
{
"value" : [
{
"data" : 1,
"name" : "quick service"
}
],
"name" : "Time on hold"
},
{
"value" : [
{
"data" : 1,
"name" : "some words but strong accent"
},
{
"data" : 1,
"name" : "EZ to understand"
}
],
"name" : "Language fluency"
},
{
"value" : [
{
"data" : 1,
"name" : "Yes"
}
],
"name" : "Human answered"
},
{
"value" : [
{
"data" : 2,
"name" : "Like a hot potato"
},
{
"data" : 1,
"name" : "1 or 2 times"
}
],
"name" : "Transfered alot"
},
{
"value" : [
{
"data" : 3,
"name" : "ok time"
},
{
"data" : 1,
"name" : "quick service"
},
{
"data" : 1,
"name" : "short time"
}
],
"name" : "Time on Hold"
},
{
"value" : [
{
"data" : 1,
"name" : "some word but strong accent"
},
{
"data" : 2,
"name" : "EZ to understand"
}
],
"name" : "Language Fluency"
},
{
"value" : [
{
"data" : 2,
"name" : "Yes"
}
],
"name" : "Human Answered"
}
],
"average" : 81
},
"cat2" : {
"detail" : [
{
"value" : [
{
"data" : 1,
"name" : "It was ok"
},
{
"data" : 1,
"name" : "Not EZ at all"
},
{
"data" : 7,
"name" : "there were hidden gimicks"
},
{
"data" : 2,
"name" : "It was OK"
},
{
"data" : 1,
"name" : "Found Quickly"
}
],
"name" : "Easy to find desired product"
},
{
"value" : [
{
"data" : 1,
"name" : "Easy to understand"
},
{
"data" : 1,
"name" : "Eventually understood"
}
],
"name" : "Clear information about the product"
},
{
"value" : [
{
"data" : 2,
"name" : "none"
},
{
"data" : 3,
"name" : "There was but won't complain"
},
{
"data" : 1,
"name" : "Very Bad"
}
],
"name" : "Annoying popups"
},
{
"value" : [
{
"data" : 4,
"name" : "Eventually Understood"
},
{
"data" : 1,
"name" : "EZ to understand"
}
],
"name" : "Clear data about the product"
}
],
"average" : 71
},
"cat3" : {
"detail" : [
{
"value" : [
{
"data" : 1,
"name" : "Very quick"
}
],
"name" : "Prompteness"
},
{
"value" : [
{
"data" : 3,
"name" : "We're cool"
},
{
"data" : 4,
"name" : "They didn't like me"
}
],
"name" : "Friendlyness"
},
{
"value" : [
{
"data" : 3,
"name" : "Very Confusing"
},
{
"data" : 2,
"name" : "Still a little confused"
},
{
"data" : 3,
"name" : "Enlightened"
}
],
"name" : "Knowledge"
},
{
"value" : [
{
"data" : 3,
"name" : "On hold too many times"
}
],
"name" : "Promptness"
}
],
"average" : 69
}
}
]
}
Hi I have been able to get the JSON response back from google places and need to extract the photo reference element from it.
I have tried to iterate over the places results and for each places results the photo_reference but i get the error message.
TypeError: Cannot read property '0' or undefined.
What I have tried:
response.on('end', function() {
var places = JSON.parse(body);
var locations = places.results;
locations.forEach(function(loc) {
var photoRef = loc.photos[0].photo_reference;
console.log('Photo ref ==' + photoRef);
});
});
Here is a sample of the JSON response.
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 46.7725141,
"lng" : 23.62686099999999
},
"viewport" : {
"northeast" : {
"lat" : 46.77284264999999,
"lng" : 23.62798785
},
"southwest" : {
"lat" : 46.77152845000001,
"lng" : 23.62348045
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
"id" : "18ce6ff7131f1ffd9ce9b2558ba407eed7409aee",
"name" : "Hotel Univers T",
"photos" : [
{
"height" : 1066,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116854836566418330262/photos\"\u003eHotel Univers T\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAE6Zi31LvtXmRM0orTBKDbJ3pgENKS82rVyP-462YhvrZAhhiDp2xowEiWsDNPU8tTmkXkhhB2ADKHi5GcptXSDx16GtG6RL9gpmogJw9BIR1JDiWSgMOzIE9xguxAQJ_s6Q9VHv77tQAmukc7JTJmBF_bFKQF3pVcmH6ffuUuiZEhDaF5BmQFqZRyeDOAA0KBkdGhSYpD-28mHr9XOKRb8dTheHJLA5bg",
"width" : 1069
}
],
"place_id" : "ChIJPzw_f2oMSUcRNl_Mz_sQS2c",
"rating" : 4,
"reference" : "CmRRAAAAGFISd-ET8V96LNII_v7yz5W80IuYIkLPyEPkf2dXWXudbM8YOfzaLDGMV1oNFl2YpN6dWEIDGl4TmAFP2wEYHPklveu-fXdFjUWA-w5cKapzxv0fdhSlSk4HkzpG9YN8EhDuT1KZUzs-O2h_eIjhgu5_GhSND6qUA9VG54QBT3csNWMaG_75Lw",
"scope" : "GOOGLE",
"types" : [
"gym",
"spa",
"lodging",
"health",
"restaurant",
"food",
"point_of_interest",
"establishment"
],
"vicinity" : "Strada Alexandru Vaida Voevod 53-55, Cluj-Napoca"
},
{
"geometry" : {
"location" : {
"lat" : 46.7720145,
"lng" : 23.6257946
},
"viewport" : {
"northeast" : {
"lat" : 46.77209325,
"lng" : 23.62638825
},
"southwest" : {
"lat" : 46.77177825,
"lng" : 23.62401364999999
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "61f80d1a74e00b55a075cacde6fc95aa88bfa83f",
"name" : "Pizza Hut Iulius Mall",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 2322,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/112054611800145890609/photos\"\u003etamara ionescu\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAOdAY1sLwHqWZ1sEFvhZrqAoFCNo7DbthSnLMn1aI6lGxmGhfJdE7dLFDsJxjwSyh_0L3F2Mf_-Si12UmJ9CtaySu6SjHrqnaoF1WdafNDNLbkDbqjvesU0FqJid9V8poKU8Hhtb2PAvRe8lkfJF6N1ujIWWOxQlRfhHdjqN_BAAEhCTFmdXtQjfWiC3qbOZO3SqGhTIsansBf4AxGYnt6FnoslOVrqtyw",
"width" : 4128
}
],
"place_id" : "ChIJ-0XLwGoMSUcR87bDPcmCLHI",
"price_level" : 1,
"rating" : 4.1,
"reference" : "CmRRAAAAby6PokNes7fY7vSBVfxZvOfZj5wehpXYD8oUFAYRgmyOwreZ-hsxByGOCb0SRRfD73muHIScP0lK7fVlMk7g2-B1tOqxHt60oDKPkfgbe42QogLNVDnAmjo1nGrtHGzUEhB9TPDl0pw_CSCpmcQkERp8GhT_kUf3Xb1CQLliMvvc_BStebrt_w",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "Strada Alexandru Vaida Voevod 55, Cluj-Napoca"
},
{
"geometry" : {
"location" : {
"lat" : 46.7731014,
"lng" : 23.627801
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "32a93ffe70bbdd2c8485e346837ad61384e67bf1",
"name" : "Hanul Dacilor",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/102010465058487677046/photos\"\u003eחוסין מוחמד\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAALGMxPZEorGPgWzFhEX8GQ6KWqvl1Gs5s4w8n0-_wyFWNQ2AaxbUXjIZK_EXnEbI6UCM4YhVyjdObdpVyEtJ6oMJ1MG_GtW_BJi2Mc-75PPBX500rAbAy_jz-8L9NL4pdC1wHF6cwWj08xx2zBHammjvvB-DD_yeSjP5qt-GV09PEhDhOXdgy57rEKjg0N5pZ01CGhTeRa3PSqE-RkpI5vGjdDObho2AwQ",
"width" : 5312
}
],
"place_id" : "ChIJmRm5HmsMSUcRLIcHKiIRKmY",
"rating" : 4.4,
"reference" : "CmRRAAAA1mC_ed2g3YhFhzsrY8fXgE4Xida9jQ9BxpIk6raJpNmFUEJn00JciB3PRisZfOCpB3034vayac5_dqY8Bp_eQnS1HBSC0IdQp_Hu4Dcb_krbWQs6Sl7bTVSD5Rsu9LqmEhCDZdOMO2YJCNCh7n3-t0qbGhTI9ibzMsLx0OQUQ_oihQkUdr_Yhg",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "Iulius Mall, Strada Alexandru Vaida Voevod 53-55, Cluj-Napoca"
},
{
"geometry" : {
"location" : {
"lat" : 46.771945,
"lng" : 23.626012
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "8d7bb32fd50d968c79c2e5048c12b95cec8cbb7d",
"name" : "Marty City",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 1125,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/114164649734885665448/photos\"\u003eMarty City\u003c/a\u003e"
],
"photo_reference" : "CoQBcwAAAAfjwkLAWX73DgVUjKgP2uS39gFUilk6CrHCfdy-V4d1ZJrdZKzHOeqNqU0f5gHy9-MWeNGsY_2q7cJpi_b9_5oYneyQWs4U-zN6l5DxpeMdtrnNuNwd31b3BA_IEy7q_JZ8IlMScrqRspSBmq6tfFaTdnNZUwr0acvEOCVnwp2MEhCw0oErjn23GJoa5knp4gZuGhQ5ztJ1RbSe1zJbhCzAqJvAm-fN-Q",
"width" : 1500
}
],
"place_id" : "ChIJU22Cv2oMSUcR-rhmfyKJixA",
"rating" : 3.8,
"reference" : "CmRRAAAAHjaKSuIN_npID0s-cC2oJAJxwLZv-sOz0J9OW8AdiRzoqlQE1MMKH3aWDwKLB9P18uGxzgefxBnkc1_E4s-4gpJsTdmoATlxSoxsuQPGl4W0pXVHBZzpnvPHCSK5GyHEEhDarUaahHk1WNi7jzExovQ3GhS-KvRprEN1uPj-cw6PJcIqiDhl8A",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "str. Alexandru Vaida Voievod 53, Cluj-Napoca"
},
Your code looks correct. Maybe you should try testing for the existence of the photos array before accessing it:
locations.forEach(function(loc) {
var photoRef;
if (loc.photos && Array.isArray(loc.photos)) {
photoRef = loc.photos[0].photo_reference;
}
else {
photoRef = "No photo in data!";
}
console.log('Photo ref == ' + photoRef);
});