std::map issue while auto sorting string as key - stdmap

I am using std::map to add values
std::map<CString,int>
the follwing is the list of items to be inserted as Key
X_1O1
X_101_A
X_70
X_67
I was expecting a sorted map with
X_67
X_70
X_101
X_101_A
but i am getting the result as
X_101
X_101_A
X_67
X_70
is there a way that I can sort the keys properly in the map?

Related

How to update values of existing keys or insert new key -values to a mongo document based on a python dictionary using pymongo?

I have a dictionary object in python,
dic = {"_id":1,
"A":123,
"B":234,
"C":222}
and a collection in mongo that has stored the same ID document in mongo which looks like,
mongodoc= {"_id":1,"A":233,"B":234,"D":999}
I want to update the documents in mongo directly based on the dictionary values using pymongo,
mongodoc={"_id":1,"A":123,"B":234,"D":999,"C":222}
If the keys matches between the dictionary and mongo document, update the values else insert new key value pairs.
I tried using
collection.update{"_id:1",{"set"}}
but this needs to be given a specific keys which doesn't work for my problem statement. Not sure how to proceed.
Kindly help.
The $set operator takes a dictionary which will achieve what you are looking to do:
collection.update_one({'_id': dic['_id']}, {'$set': dic})

Extract key value pair from JSON string dynamically (Redshift)

I am working with a column which stores the data for camera effects usage in a JSON string.
The values inside it look something like this:
{"camera": {"GIFs": ["floss_dance", "kermit"], "filters": ["blur"], "GIF_count": 2, "Filter_count": 1}}
If I want to extract data for GIFs, I use this code:
json_extract_path_text(camera_effects, 'camera', 'GIFs') which will yield the result as ["floss_dance", "kermit"]
If I want to extract particular GIF names, I use json_extract_array_element_text(camera_effects, 'camera', 'GIFs') which will give me floss_dance and kermit in separate rows.
My issue is that we keep adding new effects to our camera, which means the number of elements in the 'camera' array will keep increasing and using the json_extract_path_text(camera_effects, 'camera', 'name_of_effect') is not a dynamic code.
Is there a way to extract a list of all key:value pairs that exist for 'camera' and then use the keys as a column, and values as rows?
Note: I am using Redshift SQL

How to get the multiple key value pairs from jsonb object in postgresql?

I have column response_payload in table response as below
{"studentName":"Karate","studentStream":"API Testing", "studentAge":18}
Now I need to get the new json object based on my keys. For example, if my keys are studentName and studentStream I must get
{"studentName":"Karate","studentStream":"API Testing"}
In case if I provide invalid key, it should not bring anything related to invalid key.
I have tried json_build_object built-in function in postgresql, it is bringing invalid key and value as below
select json_build_object('invalidKey', payload -> 'invalidValue')
from response
O/P:- {"invalidKey":"null"}
I want output when invalid key and value is provided, it should not bring anything as below
O/p:- {}
You can use jsonb_strip_nulls
select
jsonb_strip_nulls(jsonb_build_object('invalidKey', payload -> 'invalidValue'))
from response

How to get keys from the value in redis

I have checked following, but didn't work.
https://redis.io/commands/keys
KEYS Room:*
1) "Room:120"
2) "Room:121"
3) "Room:122"
Following is the redis key/values (HMSET)
Room:120 [SocketId:mOQDJusPjDTBN5L-AAAC,TimeStamp:10-10-2017 12:10:00 AM]
Room:121 ....
Room:122 ....
...
Need to search as Room:* SocketId:mOQDJusPjDTBN5L-AAAC
How can I search for SocketId in the collection ?
Need to search with:
mOQDJusPjDTBN5L-AAAC
The question is not so clear
as u mentioned hmset i am assuming that you are using hashes to store your data.
As per your data,
'room120' should be the key, 'socketId' should be the field and 'mOQDJusPjDTBN5L-AAAC' should be the value.
so in order to search for socketId you can use hscan,where hscan iterates through the field of a particular key.https://redis.io/commands/scan
in case if you are just using key/value storage i.e
'socketId' being the key ,'mOQDJusPjDTBN5L-AAAC' being the value.
here u can just use the command Keys *socket*to search for the key socketId

Generate dynamic hstore key calls in Prawn

I have an hstore column that I'm using to build a table in Prawn (pdf builder). The data will consist of records for a given month. Since it is hstore, the keys used will likely change from day to day so this needs to be dynamic.
I need to determine:
1 What unique keys are used that month
I created a helper to find the unique keys that were used in the month. These will be used as column headers.
keys(#users_logs)
# this returns an array like - ["XC", "PIC", "Mountain"]
The table will display a users dutylog data for the month. For testing...If I explicitly call known hstore keys...the data displays correctly. But, since its hstore...I wont know what the table column will be in production.
For testing, I call known hstore keys...this creates the prawn table row data per duty log.
#users_logs.map do |dutylog|
[ dutylog.properties["XC"],
dutylog.properties["PIC"],
dutylog.properties["Mountain"]
]
end
But, since this is hstore...I wont know what keys to call in production. So, I need to make the above iteration dynamic.
I tried, without success, to iterate over each dutylog entry, then iterate over each unique key and output one "dutylog.properties[x]" call for each key value...but, this just outputs the array of key values. I tried using send() in the block, but that didnt help.
#users_logs.map do |dutylog|
[ keys(#users_logs).each { |k| dutylog.properties[k] }.join(",") ]
end
Any ideas on how I could make the "dutylog.properties[k]" dynamic?
Took some head scratching...but turning out to be quit easy
This will build the rows for the Prawn table
def hstore_duty_log_rows
[keys(#users_logs)] +
#users_logs.map do |dutylog|
keys(#users_logs).map { |key| dutylog.properties.keys.include?(key) ? "#{dutylog.properties[key]}" : "0" }
end
end