How to get json nested in nested array? - arrays

I need "source" with handlebars, I tried {{trailers.youtube.source}} but no response, suggestions?
"trailers" : { "quicktime" : [ ],
"youtube" : [ { "name" : "Trailer",
"size" : "Standard",
"source" : "vP9QCAcGy7Y",
"type" : "Trailer"
} ]
},

Try:
trailers.youtube[0].source
to get the first element from that array.

The solution is {{trailers.youtube.0.source}}, handlebars can't work with [].

Related

Compare array within objects in an array of the same Mongo document

I have a Mongo database with multiple documents and they all contain 2 Items for in store and online locations. The difference will be that attributes within the items object may differ. I am trying to capture all documents that have differing attributes between the object items with the array.
I've tried using $expr and comparing the position of the elements such as "Items.0.Attributes" == "Items.1.Attributes" but had no luck.
{
"ID" : "123456789",
"Items" : [
{
"ItemDept" : "softLines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts"
},
"Attributes" : [ "blue","small","mens"],
"Season" : "Summer"
"Location":"online"
}
,
{
"ItemDept" : "softlines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts")
},
"Attributes" : [ "blue","small","women"],
"Season" : "Summer"
"Location":"stores"
}
]
}
If I understand what you want to find/output, here's one way you could do it.
db.collection.find({
"$expr": {
"$not": {
"$setEquals": [
{"$first": "$Items.Attributes"},
{"$last": "$Items.Attributes"}
]
}
}
})
Try it on mongoplayground.net.

How to use mongoose find to filter by inner arrays

{
"_id" : ObjectId("5fa27539e2b1a10d0fdb82ba"),
"extra_fields" : [
{
"tag" : "alias",
"value" : "thealias"
},
{
"tag" : "name",
"value" : "nobody"
},
]
}
This is the object I'm referring to, for example, I need to query all the objects which have a tag="alias" in the extra_fields array of each object. how can I achieve this with MongoDB? Any help would be highly appreciated.
UPDATE: Please check this,
{
"_id" : ObjectId("5fa1b8ee30caf0655a64104a"),
"status" : true,
"extra_fields" : [
{
"tag" : "alias",
"value" : "hes"
},
{
"linked_collection_id" : "5fa1946a3ef5864410728f1f",
"linked_master_id" : [
"5fa19884dae0ba587c891d9a"
]
},
{
"linked_collection_id" : "5f7d68861d5a43b422b12c6d",
"linked_master_id" : [
"5f7d6c3b629ab057b67fa789"
]
}
]
}
I need to know how can I retrieve objects which has linked_collection_id='abc' AND linked_master_id contains 'xyz'. Thanks
You only need "extra_fields.tag": "alias" to compare the field and the result.
The query is quite simple:
db.collection.find({
"extra_fields.tag": "alias"
})
In this way, every document with, at least, one 'tag' field with value 'alias' will be returned.
Example here
Edit:
To find by multiple fields you can use this:
db.collection.find({
"extra_fields.linked_collection_id": "abc",
"extra_fields.linked_master_id": "xyz"
})

Query for deeply nested array in mongodb

How do I query a deeply nested docs like show in the below image.
Here columns is an array of unknown size. Each element in the column contains a record that is again an array. Each element of the record array contains an array called fields. Each entry in the field contains 2 keys called name and value.
I'm querying the name of the innermost array (in fields array). I couldn't go above level 1 order of nesting.
JSON doc of the above image
"data" : {
"columns" : [
{
"name" : "styleId",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257752"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "styleId"
}
],
"rules" : [
[
]
]
}
]
},
{
"name" : "vendorArticleNumber",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257753"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "vendorArticleNumber"
}
],
"rules" : [
[
]
]
}
]
},
{
"name" : "vendorArticleName",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257754"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "vendorArticleName"
}
],
"rules" : [
[
]
]
}
]
}
}
What can be the solutions if such kind of heaving nesting is there?
db.collection.find("data.columns.record.fields.name" : "column_mapping_form")
will match all documents where there is at least one element of columns has at least one record with at least one field where name is "column_mapping_form".
https://docs.mongodb.com/manual/tutorial/query-array-of-documents/ has very good explanation, examples, and interactive shell to play with.

MongoDB - Pull multiple objects from an array

Hi I'm trying to remove multiple objects from an array that looks like this.
{
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b3"),
"owner" : "1",
"group_name" : "PAASCU Board",
"group_members" : [
{
"faculty_name" : "Cheska Dela Rosa",
"faculty_number" : 2,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b5")
},
{
"faculty_name" : "Earl Sempio",
"faculty_number" : 7323,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b4")
},
{
"faculty_number" : 203,
"faculty_name" : "Sample",
"_id" : ObjectId("5a7dbf7952bd150a94d83958")
},
{
"faculty_number" : 8025,
"faculty_name" : "Sample Postman",
"_id" : ObjectId("5a7dc64a1cf5dd3d50167d53")
}
],
"__v" : 0 }
It works when I remove a single object using the $pull with this code.
db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
But what if I want to remove multiple objects with different faculty_number? I tried using the $each method just like how I add multiple objects in the array but it doesn't work well.
Use $in operator to pass the list of faculty values to remove documents from embedded array. More here
Try
db.groups.update(
{"_id": ObjectId("5a7da1bda21d5f3e8cf005b3")},
{"$pull":{"group_members":{"faculty_number":{$in:[8025,7323]}}}}
)

Parsing JSON array in Swift 3

I'm trying to parse JSON file to show in table view in Swift 3 without using external frameworks, but my knowledge in Swift is not enough. I have looked other questions, but nothing worked for me. The problem is that in my JSON there are couple of nested arrays.
Here is example of the JSON file:
"guides" : [
{
"name" : "First Guide",
"sections" : [
{
"title" : "Controls",
"data" : [
{
"value" : "controls.PNG",
"type" : "image"
},
{
"value" : "Sensitivity: Changes the sensitivity of the camera when turning.",
"type" : "text"
},
{
"value" : "Invert Y-Axis: Toggles inversion of camera when looking up/down.",
"type" : "text"
},
{
"value" : "crosshair.PNG",
"type" : "image"
},
{
"value" : "Lefty : Toggles the D-pad being on the left/right side of the screen.",
"type" : "text"
},
{
"value" : "Swap Jump and Sneak : Chooses whether to swap the position of jump and sneak buttons.",
"type" : "text"
},
{
"value" : "Button size - Changes the size of the buttons. Smaller buttons allow extra slots for the hotbar.",
"type" : "text"
}
]
},
{
"title" : "User Profile",
"data" : [
{
"value" : "profile.png",
"type" : "image"
},
{
"value" : "Use Cellular Data: Gives the Player the option to use cellular data.",
"type" : "text"
}
]
},
{
"title" : "Global Resources",
"data" : [
{
"value" : "resources.png",
"type" : "image"
},
..............
How I get parse the data into Swift arrays and use it to be displayed in UITableView controller. There are couple of "guides" in this JSON and I need to be able to show only one of them at the time.
Help will be much appreciated. Thank you in advance.
Try using following code:
let jsonString = "[\"a\",\"b\"]"
let jsonData = jsonString.data(using: .utf8)! as Data
do {
let dataJson = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
print(dataJson)
}
catch {
print("error getting xml string: \(error)")
}

Resources