Alamofire 5 send array of dictionaries - swift

I tried to send an array of dictionaries but the server receive another object
ejm
let param: [String: Any] = [
"info1": "test",
"info2": [
[
"material_id": 1,
"qty": 10
],
[
"material_id": 2,
"qty": 5
]
]
]
let request = AF.request(url, method: .post, parameters: params)
request.responseJSON { response in
guard response.error == nil else {
print(response.error)
return
}
print(response.value)
}
in my server when I print $_POST["info2"] the result is:
▿ value : 4 elements
▿ 0 : 1 element
▿ 0 : 2 elements
- key : material_id
- value : 1
▿ 1 : 1 element
▿ 0 : 2 elements
- key : qty
- value : 10
▿ 2 : 1 element
▿ 0 : 2 elements
- key : material_id
- value : 2
▿ 3 : 1 element
▿ 0 : 2 elements
- key : qty
- value : 5
im trying to encode the request but alamofire doesnt allow any to encode

Related

From anyObject to array of coordinates

New to Swift and json
I want to extract lng,lat from anyObject elements.
Here a part of my code to extract from json file:
for (key,value) in geoFeature! as [String: AnyObject] {print("key=",key, ptvir, value)}
let geoCoords = geoFeature?["coordinates"] as AnyObject
Resulting to :
Printing description of geoCoords:
▿ 1 element
▿ 0 : 11 elements
▿ 0 : 2 elements
- 0 : -73.596408
- 1 : 45.453657
▿ 1 : 2 elements
- 0 : -73.595466
- 1 : 45.451156
▿ 2 : 2 elements
- 0 : -73.59532
- 1 : 45.450786
▿ 3 : 2 elements
- 0 : -73.596114
- 1 : 45.450639
▿ 4 : 2 elements
- 0 : -73.596616
- 1 : 45.450549
▿ 5 : 2 elements
- 0 : -73.596746
- 1 : 45.450911
▿ 6 : 2 elements
- 0 : -73.596867
- 1 : 45.451248
▿ 7 : 2 elements
- 0 : -73.59716
- 1 : 45.452082
▿ 8 : 2 elements
- 0 : -73.597514
- 1 : 45.45307
▿ 9 : 2 elements
- 0 : -73.597638
- 1 : 45.453437
▿ 10 : 2 elements
- 0 : -73.596408
- 1 : 45.453657
From here, I did not find the code to create an array of coordinates
More details:
From a json file like this part:
{
"type": "FeatureCollection",
"name": "MtlAires2016Bref",
"features": [
{ "type": "Feature", "properties": { "ADIDU": "24661006" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.596408, 45.453657 ], [ -73.595466, 45.451156 ], [ -73.59532, 45.450786 ], [ -73.596114, 45.450639 ], [ -73.596616, 45.450549 ], [ -73.596746, 45.450911 ], [ -73.596867, 45.451248 ], [ -73.59716, 45.452082 ], [ -73.597514, 45.45307 ], [ -73.597638, 45.453437 ], [ -73.596408, 45.453657 ] ] ] } }
]
}
My code extract properties for each Feature
The problem is to extract all the polygon coordinates with the result of these 2 lines of code:
for (key,value) in geoFeature! as [String: AnyObject] {print("key=",key, ";", value)}
let geoCoords = geoFeature?["coordinates"] as AnyObject
print("geoCoords=", geoCoords as Any)
result of this print:
geoCoords= (
(
(
"-73.596408",
"45.453657"
),
(
"-73.595466",
"45.451156"
),
(
"-73.59532",
"45.450786"
),
(
"-73.596114",
"45.450639"
),
(
"-73.596616",
"45.450549"
),
(
"-73.596746",
"45.450911"
),
(
"-73.596867",
"45.451248"
),
(
"-73.59716",
"45.452082"
),
(
"-73.597514",
"45.45307"
),
(
"-73.597638",
"45.453437"
),
(
"-73.596408",
"45.453657"
)
)
)
I did not find how to iterate in geoCoords and append coordinates to an array.
Next line give me the same listing; not array
for element in geoCoords as! Array {
//print("elem=",element)}
but if I ask for the type of the variable like this
String(describing: type(of: coordsData))
it's = Array
the next line give an empty array
let tblC = coordsData as [CLLocationCoordinate2D]
Please learn to read JSON. It's pretty easy. There are only two collection types, array ([]) and dictionary ({}). So the value for key coordinates is a nested array of Double ([[[Double]]]).
It's impossible to cast a double array to CLLocationCoordinate2D because the type is not related.
And the unspecified JSON type is Any, never AnyObject
if let geoCoords = geoFeature["coordinates"] as? [[[Double]]] {
for outerCoords in geoCoords {
for innerCoords in outerCoords {
print(innerCoords)
}
}
}

Sort unsorted Dictionary or Array of dictionaries with dynamic keys

Say I have a dictionary with dynamic keys like below.
{
5 : {
value : 1000,
desp : "No Desp"
},
1 : {
value : 1000,
desp : "No Desp"
},
2 : {
value : 1000,
desp : "No Desp"
}
}
I want to sort this dictionary like below: In a swift dictionary sorted based on the key ascending order.
{
1 : {
value : 1000,
desp : "No Desp"
},
2 : {
value : 1000,
desp : "No Desp"
}},
5 : {
value : 1000,
desp : "No Desp"
}
]
The type of dictionary can not be sorted, so you need to use an array to do so.
let dict = [1:[value : 1000, desp : "No Desp"], 2: [value : 1000, desp : "No Desp"],3: [value : 1000, desp : "No Desp"]]
dict.sorted(by: { $0.0 < $1.0 }) //do this to sort it by key
The result data type is a array of [Int,[String:Int, String:String]].
I did something like this previously,
let unsortedDict = Dictioanary<String,Any>
let sortedKeys = unsortedDict.keys.sorted
var sortedDict = NSMutableDictionary ()
for key in sortedKeys {
let node = unsortedDict[key]
sortedDict.setValue(node,forKey:key)
}
wondering any better way than this

Force list type in $min update operator

I have documents with the following structure:
{
"_id" : 0,
"mins" : {
"ts1" : {
"node1" : [
1,
2,
3
],
"node2" : [
4,
5,
6
]
}
}
}
I'd like to update documents by taking the component-wise minimum for an array. As MongoDB does not support $min on arrays (I think), I'm updating each index individually like so:
db.foo.updateOne(
{"_id" : 0},
{$min: {
"mins.ts3.node1.0": 1,
"mins.ts3.node1.1": 2
}}
)
This works fine but the problem is that if the document does not have the array before updating, MongoDB creates a nested document instead of an array:
{
"_id" : 0,
"mins" : {
"ts1" : {
"node1" : [
1,
2,
3
],
"node2" : [
4,
5,
6
]
},
"ts3" : {
"node1" : {
"0" : 1,
"1" : 2
}
}
}
}
Is there a way to tell MongoDB it is updating a list even if the list does not exist yet?
I'd like to avoid creating empty lists for each document as that would break my current program design.

query document nested multiarray-array mongodb elemMatch

I have this documents:
//document 1
{
info : [
{
id : 100,
field : {
a : 1,
b : 2
}
},
{
id : 200,
field : {
a : 3,
b : 4
}
},
{
id : 300,
field : {
a : 5,
b : 6
}
}
]
},
//document 2
{
info : [
{
id : 400,
field : {
a : 7,
b : 8
}
},
{
id : 500,
field : {
a : 9,
b : 10
}
}
]
}
I need to find the id of the subdocument with the values field.a = 7 and field.b = 8 , that means the id value is 400.
What i have tried is $elemMatch but I can't get the result.
My attemps :
attemp 1:
db.mycollection.findOne({info : {$elemMatch : { 'field.$.a':7,'field.$.b':8 } } });
attemp 2:
db.mycollection.findOne({info:{$elemMatch:{$elemMatch:{'field.$.a':7,'field.$.b':8,}}}});
attemp 3:
db.mycollection.findOne({info:{$elemMatch:{$elemMatch:{'field.a.$':7,'field.b.$':8,}}}});
attemp 4:
db.mycollection.findOne({info:{$elemMatch:{'field.$.a':7,'field.$.b':8,}}});
The $elemMatch operator works like a "mini query" against the specified array element it is acting on, so arguments go inside. Also the positional $ operator here is a propery of "projection" and not the query document itself, so this is a separate element:
db.mycollection.find(
{
"info": {
"$elemMatch": { "field.a": 7 , "field.b": 8 }
}
},
{ "info.$": 1 }
)
Which both matches the document containing the matched element, and then only returns the matched element due to the projection:
{
"_id" : ObjectId("564d52979f28c6e0feabceee"),
"info" : [
{
"id" : 400,
"field" : {
"a" : 7,
"b" : 8
}
}
]
}

List all values of a certain field in mongodb

How would I get an array containing all values of a certain field for all of my documents in a collection?
db.collection:
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be6"), "x" : 1 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be7"), "x" : 2 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be8"), "x" : 3 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be9"), "x" : 4 }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990bea"), "x" : 5 }
"db.collection.ListAllValuesForfield(x)"
Result: [1,2,3,4,5]
Also, what if this field was an array?
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be6"), "y" : [1,2] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be7"), "y" : [3,4] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be8"), "y" : [5,6] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990be9"), "y" : [1,2] }
{ "_id" : ObjectId("51a7dc7b2cacf40b79990bea"), "y" : [3,4] }
"db.collection.ListAllValuesInArrayField(y)"
Result: [1,2,3,4,5,6,1,2,3,4]
Additionally, can I make this array unique? [1,2,3,4,5,6]
db.collection.distinct('x')
should give you an array of unique values for that field.
Notice: My answer is a fork from the original answer.
Before any "thumbs up" here, "thumbs up" the accepted answer :).
db.collection.distinct("NameOfTheField")
Finds the distinct values for a specified field across a single collection or view and returns the results in an array.
Reference: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/
This would return an array of docs, containing just it's x value...
db.collection.find(
{ },
{ x: 1, y: 0, _id:0 }
)
db.collection_name.distinct("key/field_name") - This will return a list of distinct values in the key name from the entire dictionary.
Just make sure you don't use the curly brackets after round brackets.