access Object/Key-value pair from dictionary - objective-c

I have the following dictionary
{
"b3e0aded-d57b-4159-9c33-c8b006282334" : {
"57646099-b717-4a2b-b9eb-2935548ae954" : [
"yes"
]
},
"1b186bc7-52c4-4b87-a97f-cc52054aad24" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
"yes"
]
},
"d765038e-e85a-495d-9932-170852fbd86e" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
"yes"
],
"57646099-b717-4a2b-b9eb-2935548ae954" : [
"yes"
],
"957bdaba-b23d-4243-8384-62dfa46f0656" : "play"
},
"2a8dd370-2f7e-4c8e-93d5-21102fbc82fd" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
"yes"
]
}
}
How to get each object say
"b3e0aded-d57b-4159-9c33-c8b006282334" : {
"57646099-b717-4a2b-b9eb-2935548ae954" : [
"yes"
]
}
OR
"d765038e-e85a-495d-9932-170852fbd86e" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
"yes"
],
"57646099-b717-4a2b-b9eb-2935548ae954" : [
"yes"
]
from this NSDictionary?

use for each in loop to get the full value. i.e
let dict = your dictionary
for each in dict {
print(each)
}

You can use objectForKey: to read the value for a key:
id value = [dict objectForKey:#"b3e0aded-d57b-4159-9c33-c8b006282334"]
Which can be expressed more concisely using square bracket notation:
id value = dict[#"b3e0aded-d57b-4159-9c33-c8b006282334"]
If you then want an object containing just that key mapped to its value then the easiest thing to do is just to construct a new object with the key and value:
id key = #"b3e0aded-d57b-4159-9c33-c8b006282334";
id value = dict[key];
id result = #{ key: value }

Related

How do i change MongoDB JSON data to array

I need to update the MongoDB field with the array of objects where JSON object to be updated with as an array
if I have something like this in MongoDB
"designSectionContents" : [
{
"_id" : "5bae17ecbd7595540145ec98",
"type" : "subSection",
"columns" : [
{
"0" : {
"itemId" : "5b7465980783d9a37058f160",
"type" : "field"
}
},
{
"0" : {
"itemId" : "5b7465630783d9a37058f15c",
"type" : "field"
}
},
{
"0" : {
"itemId" : "5b7465810783d9a37058f15e",
"type" : "field"
}
}
],
"subSectionContentLayout" : {
"labelPlacement" : "Top",
"columns" : 3
}
}
]
I want to change the above snippet to below in MongoDB
"designSectionContents" : [
{
"_id" : ObjectId("5bae17ecbd7595540145ec98"),
"type" : "subSection",
"columns" : [
[
{
"itemId" : "5b7465980783d9a37058f160",
"type" : "field"
}
],
[
{
"itemId" : "5b7465630783d9a37058f15c",
"type" : "field"
}
],
[
{
"itemId" : "5b7465810783d9a37058f15e",
"type" : "field"
}
]
]
}
]
curly braces opening and closing tag has to be changed to array
This should work:
db.collection.aggregate([
{
"$project": {
"designSectionContents": {
"$map": {
"input": "$designSectionContents",
"as": "designSectionContent",
"in": {
"_id": "$$designSectionContent._id",
"type": "$$designSectionContent.type",
"columns": {
"$map": {
"input": "$$designSectionContent.columns",
"as": "inp",
"in": [
"$$inp.0"
]
}
}
}
}
}
}
}
]);
Here's the working link.

mapper_parsing_exception in Elasticsearch(Reason: No type specified for field [X])

I wanted to provide explicit mapping to the fields in my document, So I defined a mapping for my index demo and It looks like this below:
PUT /demo
{
"mappings": {
"properties": {
"X" : {
"X" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"Sub_X" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
After running the query , I am getting error as :
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "No type specified for field [X]"
}
],
"type" : "mapper_parsing_exception",
"reason" : "Failed to parse mapping [_doc]: No type specified for field [X]",
"caused_by" : {
"type" : "mapper_parsing_exception",
"reason" : "No type specified for field [X]"
}
},
"status" : 400
}
The field X in json document looks like :
"X" : {
"X" : [
"a"
],
"Sub_X" : [
[
"b"
]
]
},
Please help me out with this elastic search mapper_parse_exception error.
What you have is called nested data type
You have X which in turn contains X and Sub_X.
Mapping:
{
"properties": {
"X": {
"type": "nested"
}
}
}
Data:
{
"X": {
"X": [
"a"
],
"Sub_X": [
[
"b"
]
]
}
}
Query:
{
"query": {
"nested": {
"path": "X",
"query": {
"bool": {
"must": [
{ "match": { "X.X": "a" }},
{ "match": { "X.Sub_X": "b" }}
]
}
}
}
}
}
It outputs the document.

mongodb query multiple pairs using $in

I have a collection names with data (_id is omitted):
{first:"John", last:"Smith"},
{first:"Alice", last:"Johnson"},
{first:"Bob", last:"Williams"},
...
and {first, last} is a unique index.
I want to find a lot of names in names like:
db.names.find({$or: [{first:"Alice", last:"Brown"}, {first:"Bob", last:"White"}, ...]}
Can I use $in instead of $or to simplify this query?
===
I know in MySQL this following query:
SELECT * FROM names WHERE (first = 'Alice' AND last = 'Brown') OR (first = 'Bob' AND last = 'White') OR ...
can be simplified as:
SELECT * FROM names WHERE (first, last) IN (('Alice', 'Brown'), ('Bob','White') OR ...)
But I am unable to find the equivalent query syntax in MongoDB.
The following query can get us the expected output:
db.names.find({
$expr:{
$in:[
{
"first":"$first",
"last":"$last"
},
[
{
"first" : "Alice",
"last" : "Johnson"
},
{
"first" : "Bob",
"last" : "Williams"
}
]
]
}
}).pretty()
Data set:
{
"_id" : ObjectId("5d81c3b7a832f81a9e02337b"),
"first" : "John",
"last" : "Smith"
}
{
"_id" : ObjectId("5d81c3b7a832f81a9e02337c"),
"first" : "Alice",
"last" : "Johnson"
}
{
"_id" : ObjectId("5d81c3b7a832f81a9e02337d"),
"first" : "Bob",
"last" : "Williams"
}
Output:
{
"_id" : ObjectId("5d81c3b7a832f81a9e02337c"),
"first" : "Alice",
"last" : "Johnson"
}
{
"_id" : ObjectId("5d81c3b7a832f81a9e02337d"),
"first" : "Bob",
"last" : "Williams"
}
Query Analysis: This query won't use the unique index you have created and I would recommend to go with the $or query.
Following are the winning plans:
$or query:
{
"stage" : "COLLSCAN",
"filter" : {
"$expr" : {
"$in" : [
{
"first" : "$first",
"last" : "$last"
},
{
"$const" : [
{
"first" : "Alice",
"last" : "Johnson"
},
{
"first" : "Bob",
"last" : "Williams"
}
]
}
]
}
},
"direction" : "forward"
}
$in query:
{
"stage" : "SUBPLAN",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "OR",
"inputStages" : [
{
"stage" : "IXSCAN",
"keyPattern" : {
"first" : 1,
"last" : 1
},
"indexName" : "first_1_last_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"first" : [ ],
"last" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"first" : [
"[\"Alice\", \"Alice\"]"
],
"last" : [
"[\"Brown\", \"Brown\"]"
]
}
},
{
"stage" : "IXSCAN",
"keyPattern" : {
"first" : 1,
"last" : 1
},
"indexName" : "first_1_last_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"first" : [ ],
"last" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"first" : [
"[\"Bob\", \"Bob\"]"
],
"last" : [
"[\"White\", \"White\"]"
]
}
}
]
}
}
}
You can see that the index is properly utilized in $or query and not used in the $in query.
For this mysql query
SELECT * FROM names
WHERE (first = 'Alice' AND last = 'Brown')
OR (first = 'Bob' AND last = 'White')
OR ...
can be use in mongodb as
db.names.find({
$or: [
{ first: "Alice", last: "Brown" },
{ first: "Bob", last: "White" },
...
]})
For below mysql query
SELECT * FROM names
WHERE (first, last) IN (('Alice', 'Brown'), ('Bob','White') OR ...)
You can not create mongodb $in query
db.names.find({
$or: [
{ first: { $in: ["John", "Alice", "Bob"] } },
{ last: { $in: ["Smith", "Johnson", "Williams" ] } },
]})
Because it pass this result too
("John", "Johnson"),("John", "Williams"),("John", "Smith"),("John", "Williams")..
the best way is $or as ask in question
db.names.find({
$or: [
{ first: "Alice", last: "Brown" },
{ first: "Bob", last: "White" },
...
]})
May be better solution present out there. I will update my answer if find any. Thanks
Yes, it's possible to use $in to query MongoDb collections based on multiple values.
Try using the below syntax:
db.names.find.({ field: { $in: [<value1>, <value2>, ... <valueN> ] } })
for example in your case it looks like:
db.names.find({first: {$in: ["Alice","Brown","Bob", "White"]}})

The argument to $each in $push must be an array but it was of type Object

I want to add the element in array but in my document conation one object :_id in that I have another filed xxxxx in that list I want to add
"_id" : ObjectId("5cf261718c81f028sd73b2f9")
"college" : {
"clzName" : "VDA",
"clzList" : [],
"List" : [],
"place" : [],
"date" : [
"college"
]
},
db.getCollection('aaa').update({"_id" : ObjectId("5cf261718c81f028db73b2f9")},
{$push:{"colllege":{$each:{"List":["dsd","jaa"]}}}})
e argument to $each in $push must be an array but it was of type Object
Try this it should work,
db.getCollection('aaa').update({"_id" : ObjectId("5cf261718c81f028db73b2f9")},
{$push:{"colllege":{$each:[["List":["dsd","jaa"]]]}}})
It will insert it like this
"college":{
[
"List":[
"dsd",
"jaa"
]
]
}

db.find vs db.aggregation to select nested array Object

I'v tried to perform the following query :
db.getCollection('fxh').find({"username": "user1", "pf.acc.accnbr" : 915177},{userid: true, "pf.pfid": true, "pf.acc.accid":true})
and my collection is the following :
{
"_id" : ObjectId("5932fd8f381d4c0a7de21942"),
"userid" : 1496513894,
"username" : "user1",
"email" : "user1#gmail.com",
"fullname" : "User 1",
"pf" : {
"acc" : [
{
"cyc" : [
{
"det" : {
"status" : "New",
"dcycid" : 1496513941
},
"status" : "New",
"name" : "QPT202017_M1",
"cycid" : 1496513940
}
],
"status" : "New",
"accnbr" : 915177,
"accid" : 1496513939
},
{
"cyc" : [
{
"det" : {
"status" : "New",
"dcycid" : 1496552643
},
"status" : "New",
"name" : "QPT202017_S8",
"cycid" : 1496552642
}
],
"status" : "New",
"accnbr" : 73497,
"accid" : 1496552641
}
],
"pfid" : 1496513935,
},
"lastupdate" : ISODate("2017-06-03T18:18:55.080Z"),
"__v" : 0
}
When I execute the query the result is the following :
{
"_id" : ObjectId("5932fd8f381d4c0a7de21942"),
"userid" : 1496513894,
"portfolio" : {
"acc" : [
{
"accid" : 1496513939
},
{
"accid" : 1496552641
}
],
"pfid" : 1496513935
}
}
And my problem is that I need to see only the concerned accid and the result returns the all accid !.
Any idea how just to return the selected accid of accnbr ?
NB : I have also tried to add $ sign at the end of my query , it
selects the right acc but it returns the all objects or I need just
only ONE returned object.
On 6/5/17
I also used the aggregate command instead of find and it get result by using this :
db.getCollection('fxh').aggregate([ { $unwind : "$pf.acc"} , { $match : {"username":"adh1", "pf.acc.accbr": 915177 } }, {$project : {_id:0, accid: "$pf.acc.accid"}}])
But could NOT get a lower level result, when I ran this :
db.getCollection('fxh').aggregate([ { $unwind : "$pf.acc.cyc"} , { $match : {"username":"adh1", "pf.acc.accbr": 915177, "pf.acc.cyc.name": "QPT202017_M1" } }, {$project : {_id:0, cycid: "$pf.acc.cyc.cycid"}}])
Any idea ?
You can try the below aggregation pipeline.
The idea is to $unwind one nested level at a time, starting from the outermost to the innermost.
For each nested level unwinding, you can apply the$match to limit the documents and continue till you have the desired shape.
You can $group it together at the end to get back to the original shape.
db.getCollection('fxh').aggregate([
{ $match : {"username":"adh1"} },
{ $unwind : "$pf.acc"} ,
{ $match : {"pf.acc.accbr": 915177 } },
{ $unwind : "$pf.acc.cyc"},
{ $match : {"pf.acc.cyc.name": "QPT202017_M1" } },
{$project : {_id:0, accid: "$pf.acc.accid", cycid: "$pf.acc.cyc.cycid"}}])