Query multiple elements in nested JSON Document - json

I have the following sample data in MongoDB:
{
"_id" : ObjectId("54833e93ade1a1521a2a2fe8"),
"fname" : "yumi",
"mname" : "sakura",
"lname" : "kirisaki",
"consultations" : [
{
"medications" : [
"paracetamol",
"ibuprofen",
"carbocisteine"
],
"diagnosis" : [
"sore throat",
"fever",
"cough"
],
"date" : ISODate("2014-12-01T16:00:00Z")
},
{
"medications" : [
"paracetamol",
"carbocisteine",
"afrin"
],
"diagnosis" : [
"cough",
"colds",
"fever"
],
"date" : ISODate("2014-12-11T16:00:00Z")
}
]
}
{
"_id" : ObjectId("54833e93ade1a1521a2a2fe9"),
"fname" : "james",
"mname" : "legaspi",
"lname" : "reyes",
"consultations" : [
{
"medications" : [
"zanamivir",
"ibuprofen",
"paracetamol"
],
"diagnosis" : [
"influenza",
"body aches",
"headache"
],
"date" : ISODate("2014-10-22T16:00:00Z")
},
{
"medications" : [
"carbocisteine",
"albuterol",
"ibuprofen"
],
"diagnosis" : [
"asthma",
"cough",
"headache"
],
"date" : ISODate("2014-11-13T16:00:00Z")
}
]
}
I am trying to query patients with zanamivir AND ibuprofen AND cough:
db.patient.find({
$and:
[
{"consultations.medications":["zanamivir", "ibuprofen"]},
{"consultations.diagnosis":"cough"}
]
}).pretty()
So, in the short sample data, I was hoping james would be returned since he is the only one with zanamivir medication.
Nothing is happening when I enter the above query in cmd. It just goes to the next line (no syntax errors, etc.)
How must I go about the query?

You need the use the $all operator.
db.patient.find({
"consultations.medications": { "$all" : [ "zanamivir", "ibuprofen" ]},
"consultations.diagnosis": "cough"
})

Pretty simple, it's just your first part of the query.
db.patient.find({
$and:[
{"consultations.medications":["zanamivir", "ibuprofen"]},
{"consultations.diagnosis":"cough"}]})
Asking Mongodb to find consultations.medications against ["zanamivir", "ibuprofen"] is asking it to find someone whose medications are equal to ['zanamivir', 'ibuprofen'].
If you want to find people who have had zanamivir and ibuprofen medicated you need to tweak the query to this:
db.patient.find({
$and:[
{"consultations.medications":"zanamivir"},
{"consultations.medications":"ibuprofen"},
{"consultations.diagnosis":"cough"}]})
Enjoy!

Related

Why do consecutive numbers in a JSON tree cause a failed conversion to NSDictionary?

I've been working with Firebase, and in a part of my JSON tree, I had a bunch of consecutive numbers as keys.
Here's an abbreviated portion of that part of the tree:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"3" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
When I downloaded it from Firebase and used this:
if let r = ref {
r.child(accessKey).child("matches").observeSingleEvent(of: .value, with: { (snapshot) in
if let groups = snapshot.value as? NSDictionary {
The conversion to NSDictionary failed.
However, when I edited "matches" to look like this:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"4" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
It worked.
I was able to convert the data to an NSDictionary.
This also worked by changing the first "1" to "01" and appending a string to the beginning of each number like "match-1", "match-2", etc.
Even though my code works, the error is still bothering me because I don't know why it was wrong in the first place.
Why didn't it work with consecutive numbers as keys in the first place?
Thanks in advance!

Mongo forEach Query

I have the JSON that you can see below and I want to sum the values of the two objects, but when I make an aggregation it returns me 0.Here you can see the query that I use; really the first line I only use it to be sure that the path works, and it does. On the other hand,when I use this path in the aggregation query it gives me the "ID" and the "COUNT" with right values,but the "SUM" is always 0 when it must be 3600.Any idea?
db.getCollection('TEST').find({"prices.year.months.day.csv.price.valPrice":1800})
db.TEST.aggregate([
{ $match: {"location.cp":"20830"}},
{$group:{_id:"20830",total:{$sum:"$prices.year.months.day.csv.price.valPrice"}, count: { $sum: 1 }
}}])
And this is the JSON:
{
"_id" : "20830:cas:S:3639",
"lodgtype" : "Casa",
"lodg" : "Motrico: country holiday home - San sebastian",
"webid" : "6107939",
"location" : {
"thcod" : "20",
"cp" : "20830",
"th" : "Gipuzkoa",
"geometry" : {
"type" : "Point",
"coordinates" : [
43.31706238,
-2.40293598
]
}
},
"prices" : {
"year" : [
{
"valYear" : "2018",
"months" : [
{
"valMonth" : "02",
"day" : [
{
"valDay" : "13",
"csv" : [
{
"valCsv" : "20180205210908_223",
"price" : [
{
"valPrice" : 1800.0
}
]
}
]
}
]
}
]
}
]
},
"reg" : {
"created" : "20180213",
"updated" : "20180213",
"viewed" : "20180213"
}
},{
"_id" : "TEST20830:cas:S:3639",
"lodgtype" : "Casa",
"lodg" : "TESTMotrico: country holiday home - San sebastian",
"webid" : "6107930",
"location" : {
"thcod" : "20",
"cp" : "20830",
"th" : "Gipuzkoa",
"geometry" : {
"type" : "Point",
"coordinates" : [
43.31706238,
-2.40293598
]
}
},
"prices" : {
"year" : [
{
"valYear" : "2018",
"months" : [
{
"valMonth" : "02",
"day" : [
{
"valDay" : "13",
"csv" : [
{
"valCsv" : "20180205210908_223",
"price" : [
{
"valPrice" : 1800.0
}
]
}
]
}
]
}
]
}
]
},
"reg" : {
"created" : "20180213",
"updated" : "20180213",
"viewed" : "20180213"
}
}
Since you've deeply nested array you've to unwind to flatten to a document structure. To count the number of matches you've to use extra group after $match with $push with $$ROOT to keep the matching data.
db.TEST.aggregate([
{"$match":{"location.cp":"20830"}},
{"$group":{
"_id":"20830",
"data":{"$push":"$$ROOT"},
"count":{"$sum":1}
}},
{"$unwind":"$data.prices.year"},
{"$unwind":"$data.prices.year"},
{"$unwind":"$data.prices.year.months"},
{"$unwind":"$data.prices.year.months.day"},
{"$unwind":"$data.prices.year.months.day.csv"},
{"$unwind":"$data.prices.year.months.day.csv.price"},
{"$group":{
"_id":"20830",
"total":{"$sum":"$prices.year.months.day.csv.price.valPrice"},
"count":{"$first":"$count"}
}}
])

Try to Intersect JSON Array

I need to Intersect my Query Result. After intersection the data I want to use it in my page.
This is my Data : (I store the data in variable "hasilget")
[
{
"_id" : "2017-05-22",
"kamar" : [
[
{
"_id" : ObjectId("570f5095c8dbf1045d7fe9b3"),
"namkam" : "VIP ONE",
"idtipe" : ObjectId("57023e5e36b35501f17ea5c6")
}
],
[
{
"_id" : ObjectId("570f509dc8dbf1035d7fe9b5"),
"namkam" : "VIP TWO",
"idtipe" : ObjectId("57023e5e36b37601f17ea5c6")
}
]
]
},
{
"_id" : "2017-05-23",
"kamar" : [
[
{
"_id" : ObjectId("570f5095c8dbf1045d7fe9b3"),
"namkam" : "VIP ONE",
"idtipe" : ObjectId("57023e5e36b35501f17ea5c6")
}
]
]
}
]
I have try Use "Array-Intersection" from NPM.
var inter = intersection(hasilget);
console.log("Hasil "+JSON.stringify(inter));
But The Problem is the output is Still Originally Result. The Intersection Function is not working.
Please guide me.

Cypher query JSON formatted result

On the Actor/Movie demo graph, cypher returns column names in a separate array.
MATCH (n:Person) RETURN n.name as Name, n.born as Born ORDER BY n.born LIMIT 5
results:
{ "columns" : [ "Name", "Born" ], "data" : [ [ "Max von Sydow", 1929 ], [ "Gene Hackman", 1930 ], [ "Richard Harris", 1930 ], [ "Clint Eastwood", 1930 ], [ "Mike Nichols", 1931 ] ]}
Is it possible to get each node properties tagged instead?
{ "nodes" : [ ["Name": "Max von Sydow", "Born": 1929 ], ...] }
If I return the node instead of selected properties, I get way too many properties.
MATCH (n:Person) RETURN n LIMIT 5
results:
{ "columns" : [ "n" ], "data" : [ [ { "outgoing_relationships" : "http://localhost:7474/db/data/node/58/relationships/out", "labels" : "http://localhost:7474/db/data/node/58/labels", "data" : { "born" : 1929, "name" : "Max von Sydow" }, "all_typed_relationships" : "http://localhost:7474/db/data/node/58/relationships/all/{-list|&|types}", "traverse" : "http://localhost:7474/db/data/node/58/traverse/{returnType}", "self" : "http://localhost:7474/db/data/node/58", "property" : "http://localhost:7474/db/data/node/58/properties/{key}", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/58/relationships/out/{-list|&|types}", "properties" : "http://localhost:7474/db/data/node/58/properties", "incoming_relationships" : "http://localhost:7474/db/data/node/58/relationships/in", "extensions" : { }, "create_relationship" : "http://localhost:7474/db/data/node/58/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/58/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/58/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/58/relationships/in/{-list|&|types}" } ], ... ]}
You can use the new literal map syntax in Neo4j 2.0 and do something like:
MATCH (n:Person)
RETURN { Name: n.name , Born: n.born } as Person
ORDER BY n.born
LIMIT 5

Corinis/JSFORM : nested collections are ignored

I'm using Corinis/jsForm.js (jquery.jsForm-1.0.rc.2.js) to map JSON to/from html fields
I have trouble when my collection is deeply nested
(i.e. the field seems to get ignored)
any suggestions ?
My json object looks like this
{ "#class" : "CmsNode",
"#fieldTypes" : "createdOn=t,updatedOn=t",
"#rid" : "#13:16",
"#type" : "d",
"#version" : 18,
"children" : [ ],
"cmsRecord" : { "#class" : "CmsRecord",
"#type" : "d",
"#version" : 0,
"active" : [ { "#class" : "Record",
"#type" : "d",
"#version" : 0,
"properties" : { "alias" : "blah" }
} ],
"archive" : [ ],
"classifications" : [ ],
"keywords" : [ ],
"pending" : [ ]
},
"createdOn" : "2013-07-05 12:38:59:057",
"data" : { "name" : "test3en" },
"history" : [ ],
"isMenu" : true,
"isVisible" : false,
"mvcModel" : "dd",
"mvcView" : "aaa",
"pageViews" : [ ],
"parents" : [ "#13:9" ],
"related" : [ ],
"updatedOn" : "2013-07-08 09:06:47:610",
"uuid" : "933a10da-b9a8-44d1-9a65-adc189c740b2",
"viewClasses" : [ ]
}
I need to have an input field attached to property data.cmsRecord.active.properties.alias
I'm trying to achieve this, by using the following html code:
<div class="collection" data-field="data.cmsRecord.active">
<div>
<input type="text" name="active.properties.alias" />
</div>
</div>
Issue has been resolved by the script author.
fixed Release: jsForm-1.0.3.js