Add New key at the end of each object inside an array - javascript

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
})
}}
)
})

Related

fetch data which come from firebse

I just wanted to fetch that data which coming from this https://food-ordring-af14d-default-rtdb.firebaseio.com/order the data is in the form of JSON and it looks like that
{
"-MzJLd0sSssh9tbJbjUS" : {
"orderDetails" : [ {
"amount" : 4,
"id" : "m1",
"name" : "Shushi",
"price" : 22.99
}, {
"amount" : 3,
"id" : "m2",
"name" : "Schnitzel",
"price" : 16.5
} ],
"user" : {
"address" : "A-132 shiv vihar,rishal garden,Nangloi",
"city" : "New delhi",
"name" : "Bhupender Sharma",
"postal" : "110041"
}
},
"-MzJOf_52xhRQi3izTRV" : {
"orderDetails" : [ {
"amount" : 2,
"id" : "m3",
"name" : "Barbecue Burger",
"price" : 12.99
}, {
"amount" : 2,
"id" : "m4",
"name" : "Green Bowl",
"price" : 18.99
} ],
"user" : {
"address" : "Nangloi",
"city" : "New delhi",
"name" : "ANkit",
"postal" : "445575"
}
}
}
Try this code .. here some example
const fetchdata = async()=>{
const response = await fetch('https://jsonplaceholder.typicode.com/todos')
const jdata = await response.json()
console.log(jdata)
}
fetchdata()

Mongoose $in operator not working in node

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"]}})

cannot use the part (stats of stats.cat3.detail.name) to traverse the element. nested objects and arrays

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
}
}
]
}

Iterating over google places JSON response

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);
});

unable to "skip" mongoose sub-documents

I am using mongoose and node and I am trying to paginate data from sub-documents. I am able to limit subdocuments, but not skip them.
The versions I am using are:
Mongo 3.0.0
Node 0.10.33
Mongoose 3.9.7
The Data
[{
"name" : "Ranger Table",
"_id" : ObjectId("550234d3d06039d507d238d8"),
"body" : [
{
"name" : "Jason",
"colour" : "Red",
"animal" : "T-rex",
"_id" : ObjectId("550234d3d06039d507d238de")
},
{
"name" : "Billy",
"colour" : "Blue",
"animal" : "Triceratops",
"_id" : ObjectId("550234d3d06039d507d238dd")
},
{
"name" : "Zach",
"colour" : "Black",
"animal" : "Mastadon",
"_id" : ObjectId("550234d3d06039d507d238dc")
},
{
"name" : "Tommy",
"colour" : "Green",
"animal" : "Dragon"
"_id" : ObjectId("550234d3d06039d507d238d9")
}
]
},{
"name" : "Bot Table",
"_id" : ObjectId("5502d205184cd74033f64e6b"),
"body" : [
{
"name" : "Optimus",
"team" : "Autobots",
"class" : "Leader",
"_id" : ObjectId("550234d3d06039d507d238d9")
},
{
"name" : "Bumblebee",
"team" : "Autobots",
"class" : "Scout",
"_id" : ObjectId("550234d3d06039d507d238da")
},
{
"name" : "Astrotrain",
"team" : "Decepticons",
"class" : "Transport",
"_id" : ObjectId("550234d3d06039d507d238db")
}
]
}]
The Code
var BodySchema = new Schema({random: String},{strict:false});
var FeedSchema = new Schema({
name: String,
body:[BodySchema]
});
var feed = mongoose.model('Feed', FeedSchema);
feed.find({_id:'550234d3d06039d507d238d8'})
.populate({
"path":"body",
"options":{
limit:2, //This works fine
skip:2 //This doesn't work
}
})
.exec(function(err, result){
if(err){return(res.send(500, err))}
res.send(result);
});
The Result
The above code DOES limit the number of "body" sub-documents to 2, but doesn't skip any.
The code above returns this:
{
"name" : "Ranger Table",
"_id" : ObjectId("550234d3d06039d507d238d8"),
"body" : [
{
"name" : "Jason",
"colour" : "Red",
"animal" : "T-rex",
"_id" : ObjectId("550234d3d06039d507d238de")
},
{
"name" : "Billy",
"colour" : "Blue",
"animal" : "Triceratops",
"_id" : ObjectId("550234d3d06039d507d238dd")
}
]
}
But it should return this:
{
"name" : "Ranger Table",
"_id" : ObjectId("550234d3d06039d507d238d8"),
"body" : [
{
"name" : "Zach",
"colour" : "Black",
"animal" : "Mastadon",
"_id" : ObjectId("550234d3d06039d507d238dc")
},
{
"name" : "Tommy",
"colour" : "Green",
"animal" : "Dragon"
"_id" : ObjectId("550234d3d06039d507d238d9")
}
]
}
The solution that I found was to use aggregate and specify the name of the subdocument with $unwind.
http://docs.mongodb.org/manual/reference/operator/aggregation/
feed.aggregate([
{'$match':{_id:id('550234d3d06039d507d238d8')}},
{'$unwind':'$body'},
{'$skip':2},
{'$limit':2},
], function(err, result){
if(err){return(res.send(500, err))}
res.send(result);
});

Categories