Sort unsorted Dictionary or Array of dictionaries with dynamic keys - swift

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

Related

Firebase order by child returning results that do not have the child

I have a firebase data structure like the following for direct message chats:
{
"dms" : {
"meta" : {
"1-2" : {
"lastMessage" : "Inadvertently",
"lastMessageFrom" : 5,
"lastMessageKey" : "-KhUHUqK9WfUJcjs2fQ-",
"timestamp" : 1491952336485,
"users" : {
"uid-1" : {
"lastMessageRead" : {
"key" : "-KhStVBVIfRGepiWt6cn"
}
},
"uid-2" : {
"lastMessageRead" : {
"key" : "-KhSrksAgg_2TqmcLjsz"
}
}
}
}
}
}
}
If the user id is 1, I only want to retrieve nodes where users.uid-1 exists. In Swift, I thought this looked like:
FIRDatabase.database().reference().child("dms").child("meta").queryOrdered(byChild: "users/uid-3").observe(.value, with: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
...
but that is returning every dms node. For example, I am querying for uid-3 above but it will return the node in the data structure example I provided. What am I doing wrong?
I ended up changing my structure to this:
{
"meta" : {
"1-2" : {
"lastMessage" : "Dude....",
"lastMessageFrom" : 2,
"lastMessageKey" : "-KhXpXDn7et6mgFJRkWb",
"timestamp" : 1492011852859,
"users" : {
"uid-1" : true,
"uid-2" : true
},
"users-meta" : {
"uid-2" : {
"lastMessageRead" : {
"key" : "-KhXpXDn7et6mgFJRkWb"
}
}
}
}
}
}
And my query to require that the child value equal true. I guess in their docs when they explain how null values will be ordered first, they mean null or missing?

MongoDB query to return documents that only have keys amongst a predefined set

The MongoDB query language allows filtering documents based on the existence or absence of a given field with the $exists operator.
Is there a way, with the MongoDB syntax, and given a set K of allowed fields, to exclude documents that have fields not in K from the results, but:
not knowing in advance which extra fields (outside K) can be encountered
not using JavaScript, that is, the $where operator?
Example:
{
"Some field" : "foo"
}
{
"Some field" : "bar",
"Some other field" : "foobar"
}
With the set K = [ "Some field" ], only the first document is to be returned.
Note how this is not to be confused with a projection, which would return both documents but removing the extra field.
I'm not sure if MongoDB do support such kind of operations out of box but you can achieve so with help of mapReduce.
Assuming your sample data set;
// Variable for map
var map = function () {
var isAcceptable = true;
Object.keys(this).forEach(function (key) {
if (key != "_id" && white_list.indexOf(key) == -1) {
isAcceptable = false;
}
});
if (isAcceptable == true) {
emit(1, this);
}
};
// Variable for reduce
var reduce = function (key, values) {
return values;
};
db.collection.mapReduce(
map,
reduce,
{
scope: {"white_list": ["Some field"]},
out: {"inline": 1}
}
);
Will return:
{
"results" : [
{
"_id" : 1,
"value" : {
"_id" : ObjectId("57cd7503e55de957c62fb9c8"),
"Some field" : "foo"
}
}
],
"timeMillis" : 13,
"counts" : {
"input" : 2,
"emit" : 1,
"reduce" : 0,
"output" : 1
},
"ok" : 1
}
Desired result will be in results.values of returned document. However, keep in mind limitation of MongoDB mapReduce and maximum size of BSON document.
Given a set of known fields K, you can construct a query that takes the set as input and gives a query with the $exists operator along with the corresponding fields projection. Using an example, suppose you have the following documents in a test collection
db.test.insert({ "fieldX": "foo", "fieldY": "bar", "fieldZ": 1 })
db.test.insert({ "fieldX": "123", "fieldY": "bar", "fieldZ": 2 })
db.test.insert({ "fieldY": "abc", "fieldZ": 3 })
db.test.insert({ "fieldX": "xyz", "fieldZ": 4 })
db.test.insert({ "fieldZ": 5 })
Then you can construct a query Q and a projection P from an input set K as follows:
var K = [ "fieldX", "fieldZ" ];
var or = K.map(function(field) {
var obj = {};
obj[field] = { "$exists": true };
return obj;
});
var P = K.reduce(function(doc, field) {
doc[field] = 1;
return doc;
}, {} );
var Q = { "$or": or };
db.test.find(Q, P);
Sample Output:
/* 1 */
{
"_id" : ObjectId("57cd78322c241f5870c82b7d"),
"fieldX" : "foo",
"fieldZ" : 1
}
/* 2 */
{
"_id" : ObjectId("57cd78332c241f5870c82b7e"),
"fieldX" : "123",
"fieldZ" : 2
}
/* 3 */
{
"_id" : ObjectId("57cd78332c241f5870c82b7f"),
"fieldZ" : 3
}
/* 4 */
{
"_id" : ObjectId("57cd78332c241f5870c82b80"),
"fieldX" : "xyz",
"fieldZ" : 4
}
/* 5 */
{
"_id" : ObjectId("57cd78332c241f5870c82b81"),
"fieldZ" : 5
}

Are there different solution how retrieve waypoints?

"-KHbCuQflKHrJiUmfpG0" : {
"waypoints" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "jamesrick",
"title" : "Highway",
"type" : "polyline"
},
I have this structure for lines in Firebase. How retrieve all data with also nested node waypoints?
ref.observeEventType(.Value, withBlock: { polylines in
if let objects = polylines.children.allObjects as? [FDataSnapshot] {
for object in objects {
polylineDictionary = object.values as? Dictionary<String, AnyObjects> {
// ? ? ?
}
}
}
})
Now I have access to title, subtitle, type but how obtain access to waypoints? When I use
`polylineDictionary["waypoints"] as? [String: [String:Double]]`
so this dictionaries are not ordered. Thanks for some advice.
This is how I would get the nested waypoints... it's basically by iterating through the keys... I would also add some error checking when grabbing the longitude and latitudes to make sure they're there, but this is the gist:
if let polylineDictionary["waypoints"] as? [String: [String:Double]] {
let waypoints = Array(polylineDictionary.keys)
for i in 0..<waypoints.count {
let waypointId = waypoints[i]
let latitude = polylineDictionary[waypointId]["latitude"]
let longitutde = polylineDictionary[waypointId]["longitude"]
}
}
It's not clear from the question about ordering; if you just want to get to the waypoints, it's pretty straight forward:
Assume your complete Firebase structure is:
root_node
"-KHbCuQflKHrJiUmfpG0" : {
"waypoints" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "jamesrick",
"title" : "Highway",
"type" : "polyline"
}
Suppose I want the data for this specific node, -KHbCuQflKHrJiUmfpG0
let nodeRef = rootRef.childByAppendingPath("-KHbCuQflKHrJiUmfpG0")
nodeRef.observeSingleEventOfType(.Value , withBlock: { snapshot in
print(snapshot.value) //prints everything in the node
let title = snapshot.value["title"] as! String //to get any element
print(title) //prints Highway
var waypoints = [String: [String:String]]() //dict to hold key:value, unordered
waypoints = snapshot.value["waypoints"] as! Dictionary
print(waypoints) //prints the 3 waypoints and their children (as a dict)
//get fancy and convert the dictionary to an array (which can be sorted)
let arr = waypoints.map {"\($0) \($1)"}
for point in arr {
print(point)
}
}

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

MongoDB Map - Reduce result not exactly

I had data structure in MongoDB as below
{
"_id" : ObjectId("523aab00045624a385e5f549"),
"name" : "English Book 29",
"SKU" : 1000549081,
"price" : 249000,
"image" : null,
"category_id" : ObjectId("523a7802b50418baf38b4575"),
"category_name" : "English Book",
"details" : {
"Title" : "Title 549081",
"Binding" : 1,
"Author" : "Author 0",
"Publication data" : 0.5263832447608386,
"Publisher name" : "Publisher name 14",
"Number of page" : 90
}
}
Binding of book has 2 values:
0 that means soft binding, and 1 that means hard binding. I write Map Reduce to statistics for each values.
var map = function()
{
for(var key in this.details)
{
if(key == 'Binding')
{
emit({name: key}, {
'data':
[
{
name: this.details[key],
count: 1
}
]
});
}
}
};
var reduce = function (key, values) {
var reduced = {};
for(var i in values)
{
var inter = values[i];
for(var j in inter.data)
{
if(typeof(reduced[inter.data[j].name]) != "undefined")
{
reduced[inter.data[j].name] += inter.data[j].count;
}
else
{
reduced[inter.data[j].name] = 1;
}
}
}
return reduced;
};
When I run with small data (50 records) result return exactly. But when I run it with real data (192000 records) result return Not exactly. The result as below
{
"_id" : {
"name" : "Binding"
},
"value" : {
"0" : 50,
"1" : 50
}
}
I checked return data when Map/Reduce done, result as below
"counts" : {
"input" : 192000,
"emit" : 192000,
"reduce" : 1920,
"output" : 1
},
What wrong with it. Welcome any suggestion, explanation.
Thanks and best regards,
After researching about Map/Reduce yesterday, I realized that, "Emit" send 100 elements once, and "Reduce" perform on this data set. So my above code is wrong because it only "SUM" on small data set.
Below that is my new code for Map-Reduce
var map = function ()
{
for(var key in this.details)
{
if(key == 'Binding')
{
var value = {};
value[this.details[key]] = 1;
emit(key, value);
}
}
}
var reduce = function (key, values)
{
var reduced = {};
for(var idx = 0; idx < values.length; idx++)
{
var inner = values[idx];
for (var j in inner)
{
if (typeof (reduced[j]) == 'undefined')
{
reduced[j] = 0;
}
reduced[j] += inner[j];
}
}
return reduced;
}
I post here for anyone who meet similar situation. Thanks for reading.