Converting Firebase data into Swift array - swift

This is how my Firebase data looks like:
[ {
"adress" : "Högåsstigen 10 332 33 Gislaved",
"helg" : "18:00-02:00",
"latitude" : 57.2985,
"longitude" : 13.54326,
"namn" : "Restaurang Åsen",
"outlet" : 0,
"phone" : "0371-123456",
"star" : 0,
"tag" : "Restaurang",
"vardag" : "10:00-19:30"
}, {
"adress" : "Högåsstigen 12 332 33 Gislaved",
"helg" : "18:00-02:00",
"latitude" : 57.9985,
"longitude" : 13.94326,
"namn" : "Kalles ställe",
"outlet" : 0,
"phone" : "0371-123456",
"star" : 2,
"tag" : "Restaurang",
"vardag" : "10:00-19:30"
}, {
"adress" : "Högåsstigen 15 332 33 Gislaved",
"helg" : "18:00-02:00",
"latitude" : 55.603384,
"longitude" : 13.020619,
"namn" : "Olles Pub",
"outlet" : 0,
"phone" : "0371-123456",
"star" : 1,
"tag" : "Krog",
"vardag" : "10:00-19:30"
} ]
I want to grab "adress" from the database.
I'm having problem to convert the data I grab from Firebase to append into an array so I can use them later. I get the error "Value of type 'String' has no member 'generator'".
I have no idea how to proceed in my coding, any suggestions?
var adresserArray = [String]()
let ref = Firebase(url: "https://HIDINGMYURL.firebaseio.com/")
ref.observeEventType(.ChildAdded, withBlock: {snapshot in
let adresser = (snapshot.value.objectForKey("adress")) as! String!
for add in adresser{
adresserArray.append(adresser)
}
})

You are trying to iterate over String here
let adresser = (snapshot.value.objectForKey("adress")) as! String!
for add in adresser{
adresserArray.append(adresser)
}
try just replace this code with
let adresser = (snapshot.value.objectForKey("adress")) as! String
adresserArray.append(adresser)

Related

Spring - return nested documents using aggregation

i have 2 class
class Team
{
#Id
var lateint id : String
var name : String? = null
var stadium : String? = null
var players : List<Players>? = null
}
class Player
{
#Id
var lateint id : String
var firstName : String? = null
var lastName : String? = null
var post : PlayerPost? = null
var number : Int? = null
}
i am trying to get a filtered list of player of a specific team, but i always get a wrong result.
this is my request
val teamCriteria = Criteria.where("_id").`is`(teamId)
val postCriteria = Criteria.where("post").`is`("DEFENDER")
val aggregation = Aggregation.newAggregation(
Aggregation.match(teamCriteria),
Aggregation.unwind("players"),
Aggregation.match(postCriteria),
)
return mongoDb.aggregate(aggregation, Team::class.java, Player::class.java).mappedResults
The problem is instead of getting a list of players as result, i get the team duplicated as the number of expected players..
expected result
[
{
"id" : "id1",
"firstName" : "player1"
"lastName" : "player1",
"number" : 2,
"post" : "DEFENDER"
},
{
"id" : "id2",
"firstName" : "player2"
"lastName" : "player2",
"number" : 3,
"post" : "DEFENDER"
}
]
i always get this result
[
{
"id" : "id1",
"name" : "team name"
"stadium" : "stadium name",
"players" : []
},
{
"id" : "id1",
"name" : "team name"
"stadium" : "stadium name",
"players" : []
}
]
Can sameone tell me what's wrong in my code?

Mongodb put Documents array as the same level

I have this array of documents, I would like to put "table" on the same level like mastil_antenas and other variables. how Can I do that with aggregate?
I'm trying with the aggregate $project but I can't get the result.
Example of Data
[ {
"mastil_antena" : "1",
"nro_platf" : "1",
"antmarcmast" : "ANDREW",
"antmodelmast" : "HWXXX6516DSA3M",
"retmarcmast" : "Ericsson",
"retmodelmast" : "ATM200-A20",
"distmast" : "1.50",
"altncramast" : "41.30",
"ORIENTMAG" : "73.00",
"incelecmast" : "RET",
"incmecmast" : "1.00",
"Feedertypemast" : "Fibra Optica",
"longjumpmast" : "5.00",
"longfo" : "100",
"calibrecablefuerza" : "10 mm",
"longcablefuerza" : "65.00",
"modelorruantena" : "32B66A",
"tiltmecfoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114934746000.jpg",
"tiltmecfoto_fh" : "2017-10-18T05:51:22Z",
"az0foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115012727000.jpg",
"az0foto_fh" : "2017-10-18T05:55:21Z",
"azneg60foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115016199000.jpg",
"azneg60foto_fh" : "2017-10-18T05:55:36Z",
"azpos60foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115020147000.jpg",
"azpos60foto_fh" : "2017-10-18T05:55:49Z",
"etiqantenafoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114920853000.jpg",
"etiqantenafoto_fh" : "2017-10-18T05:56:01Z",
"tiltelectfoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114914236000.jpg",
"tiltelectfoto_fh" : "2017-10-18T05:56:13Z",
"idcablefoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114900279000.jpg",
"idcablefoto_fh" : "2017-10-18T05:56:38Z",
"rrutmafoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114947279000.jpg",
"rrutmafoto_fh" : "2017-10-18T05:56:49Z",
"etiquetarrufoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114954648000.jpg",
"etiquetarrufoto_fh" : "2017-10-18T05:57:02Z",
"rrutmafoto1" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114959738000.jpg",
"rrutmafoto1_fh" : "2017-10-18T05:57:12Z",
"etiquetarrufoto1" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115005545000.jpg",
"etiquetarrufoto1_fh" : "2017-10-18T05:57:27Z",
"botontorre4" : "sstelcel3",
"table" : { /* put all varibles one level up*/
"tecmast" : "LTE",
"frecmast" : "2100",
"secmast" : "1",
"untitled440" : "Salir"
},
"comentmast" : "",
"longfeedmast" : "",
"numtmasmast" : "",
"otra_marca_antena" : "",
"otro_modelo_antena" : ""
}]
Starting from MongoDB version 3.4 you could use $addFields to do this.
//replace products with what makes sense in your database
db.getCollection('products').aggregate(
[
{ //1 add the properties from subdocument table to documents
$addFields: {
"documents.tecmast" : "documents.0.table.tecmast",
"documents.frecmast" : "documents.0.table.frecmast",
"documents.secmast" : "documents.0.table.secmast",
"documents.untitled440" : "documents.0.table.untitled440"
}
},
{
//(optional) 2 remove the table property from the documents
$project: {"documents.table" : 0}
}
]
)
Step 1: use $addFields to grab properties from table inside documents.table and put them on documents
Step 2: (optional) remove property "table" from documents.
I hope this helps!!!

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

Matlab - Convert char to cell

I have this output from urlread function:
// [
{
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "148.59"
,"l_fix" : "148.59"
,"l_cur" : "148.59"
,"s": "0"
,"ltt":"1:13PM EDT"
,"lt" : "May 5, 1:13PM EDT"
,"lt_dts" : "2017-05-05T13:13:23Z"
,"c" : "+2.06"
,"c_fix" : "2.06"
,"cp" : "1.41"
,"cp_fix" : "1.41"
,"ccol" : "chg"
,"pcls_fix" : "146.53"
,"eo" : ""
,"delay": ""
,"op" : "146.76"
,"hi" : "148.91"
,"lo" : "146.76"
,"vo" : "-"
,"avvo" : "-"
,"hi52" : "148.91"
,"lo52" : "89.47"
,"mc" : "771.93B"
,"pe" : "17.38"
,"fwpe" : ""
,"beta" : "1.21"
,"eps" : "8.55"
,"shares" : "5.21B"
,"inst_own" : "63%"
,"name" : "Apple Inc."
,"type" : "Company"
}
]
My question is how can I convert this to a two-column cell? Or even better create a structure called AAPL which gives me , for example for AAPL.l the price?
Use jsondecode function to convert JSON format text to a MATLAB struct type. Typically the text would start with a '[' or '{'. You can try code using a simpler subset as below.
jsondecode('{"id": "22144","t" : "AAPL","e" : "NASDAQ","l" : "148.59"}')
This produces a struct with the following fields.
id: '22144'
t: 'AAPL'
e: 'NASDAQ'
l: '148.59'

how manipulate a NSDictionary generated by a json file in swift

I've a NSDictionary populated by a JSON file.
JSON file content (initially)
{
"length" : 0,
"locations" : []
}
I want add some elements in "locations". The elements have the below structure:
[
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
]
In next code I read de JSON File
let contentFile = NSData(contentsOfFile: pathToTheFile)
let jsonDict = NSJSONSerialization.JSONObjectWithData(contentFile!, options: nil, error: &writeError) as NSDictionary`
like you can see jsonDict contain the JSON's info but in a NSDictionary object.
At this point I can't add the elements mentioned before, I tried insert NSData, NSArray, Strings, and nothing results for me
After do this I want convert "final" NSDictionary in JSON again to save it in a file.
The "final" NSDictionary must be like this
{
"length" : 3,
"locations" : [
{
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
},
{
"name" : "some_name_2",
"lat" : "8.88889",
"long" : "9.456789",
"date" : "19/01/2015"
},
{
"name" : "some_name_3",
"lat" : "67.88889",
"long" : "5.456789",
"date" : "19/01/2015"
}
]
}
"length" control the index for new element
I have no more ideas to do this. thanks in advance
If you want to be able to modify the dictionary, you can make it mutable:
let jsonDict = NSJSONSerialization.JSONObjectWithData(contentFile!, options: .MutableContainers, error: &writeError) as NSMutableDictionary
The resulting NSMutableDictionary can be modified. For example:
let originalJSON = "{\"length\" : 0,\"locations\" : []}"
let data = originalJSON.dataUsingEncoding(NSUTF8StringEncoding)
var parseError: NSError?
let locationDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &parseError) as NSMutableDictionary
locationDictionary["length"] = 1 // change the `length` value
let location1 = [ // create dictionary that we'll insert
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
]
if let locations = locationDictionary["locations"] as? NSMutableArray {
locations.addObject(location1) // add the location to the array of locations
}
If you now constructed JSON from the updated locationDictionary, it would look like:
{
"length" : 1,
"locations" : [
{
"long" : "5.456789",
"lat" : "4.88889",
"date" : "19/01/2015",
"name" : "some_name"
}
]
}