Find document in a hierarchical MongoDB collection by any field - mongodb-query

Given the sample document below in MongoDB, is there a way to find all the documents that have someone with the name "John"? That is, the query would return records 1 and 3 below. I can't use {"name" : "John" } or {"father.name" : "John" } as both only return a single record.
{ "id": 1, "name": "John" },
{ "id": 2, "name": "Sam" },
{ "id": 3, "name": "Fred", "father": { "name": "John" } }

Related

How to match field value in response when there are multiple fields with the same name?

[
{
"key": "test1",
"category": "test",
"name": "test1",
"translations":
{
"english": "eng"
}
},
{
"key": "test2",
"category": "test",
"name": "test1",
"translations":
{
"english": "eng2",
"german": "German"
}
},
{
"key": "test3",
"category": "power",
"name": "test1",
"translations":
{
"EN_lang": "jik"
}
}
]
Here, we have multiple field's are with different values and we have to match value in translations (field position will change on every call)
You have to be clear about what you want to assert. Hint, the new contains deep (available in 0.9.6.RC4) can help:
* match response contains deep { key: 'test2', translations: { english: 'eng2' } }
Else you should look at transforming the JSON into a shape where it is easier to do the assertions you want: https://github.com/intuit/karate#json-transforms

How do I write a SQL query for a shown azure cosmos db documents?

I have following documents in azure cosmos db collection.
// Document 1
{
"c": {
"firstName": "Robert"
}
"elements" : [
{
"a": "x2",
"b": {
"name": "yadda2",
"id": 1
}
}
]
}
// Document 2
{
"c": {
"firstName": "Steve"
}
"elements" : [
{
"a": "x5",
"b": {
"name": "yadda2",
"id": 4
}
},
{
"a": "x3",
"b": {
"name": "yadda8",
"id": 5
}
},
]
}
// Document 3
{
"c": {
"firstName": "Johnson"
}
"elements" : [
{
"a": "x4",
"b": {
"name": "yadda28",
"id": 25
}
},
{
"a": "x5",
"b": {
"name": "yadda30",
"id": 37
}
},
]
}
I need to write a query that returns all documents which have "b" object whose name is "yadda2" (i.e. /elements/*/b/name=yadda2). In other words, this query should return document 1 and 2 but NOT 3.
I tried following but it did not work:
SELECT * FROM x where ARRAY_CONTAINS(x.elements, {b: { name: "yadda2"}})
What am I doing wrong?
Just modify your sql to :
SELECT * FROM x where ARRAY_CONTAINS(x.elements, {b: { name: "yadda2"}},true)
Result:
Based on the official doc , the boolean expression could specify if the match is full or partial.
Hope it helps you.

How to know whether a email address exists on Elasticsearch?

So I have added some data (email address with name) on elasticsearch . Now want to verify a particular email address is exists or not .
Below is the code which I have Executed via Postman :
**URL :** localhost:9200/demo1/demo_emails/_bulk (Put request)
**Raw Data (json) :**
{ "create" : { "_index" : "demo1", "_type" : "Supp_emails", "_id" : "2" } }
{ "name" : "x1", "email": "x1#r.com" }
{ "create" : { "_index" : "demo1", "_type" : "Supp_emails", "_id" : "3" } }
{ "name" : "x2", "email": "x2#r.com" }
You can see "demo1" is the Index & "demo_emails" is the field type . And I have added two email address on that index.
Now want to verify whether 'x1#r.com' is exist or not ?
I have tried the below query, but its showing all details instead of one email
**URL :** localhost:9200/demo1/Supp_emails/_search?q=email:x1#r.com (Get request)
**Output :**
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.87546873,
"hits": [
{
"_index": "demo1",
"_type": "demo_emails",
"_id": "1",
"_score": 0.87546873,
"_source": {
"name": "x1",
"email": "x1#r.com"
}
},
{
"_index": "demo1",
"_type": "demo_emails",
"_id": "2",
"_score": 0.87546873,
"_source": {
"name": "x2",
"email": "x2#r.com"
}
}
When you search for email:x1#r.com the tokenizer breaks up the string into tokens. These tokens depend on which analyzer and tokenizer you've used.
I'm assuming you didn't do any fancy analysis, so these tokens would be :
GET index/_analyze
{
"tokens": [
{
"token": "x1",
"start_offset": 0,
"end_offset": 2,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "r.com",
"start_offset": 3,
"end_offset": 8,
"type": "<ALPHANUM>",
"position": 1
}
]
}
If you take a look at the documentation of query_string you'll find that default operator is OR
So your query actually looks for x1 OR r.com, this is why both of these documents return.
You can see this by adding the parameter http://elastic...?q=...&explain=true.
How do you solve this issue? Use a different operator. Just change the default operator to AND, d http://elastic...?q=...&default_operator=true
Useful links:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices-analyze.html

Transform JSON response with lodash

I'm new in lodash (v3.10.1), and having a hard time understanding.
Hope someone can help.
I have an input something like this:
{
{"id":1,"name":"Matthew","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
{"id":2,"name":"Mark","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
{"id":3,"name":"Luke","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
{"id":4,"name":"John","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
{"id":5,"name":"Paul","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}}
];
I would like to output this or close to this:
{
"industries": [
{
"industry":{
"id":5,
"name":"Medical",
"companies": [
{
"company":{
"id":1,
"name":"abc",
"employees": [
{"id":1,"name":"Matthew"},
{"id":2,"name":"Mark"},
{"id":3,"name":"Luke"},
{"id":4,"name":"John"},
{"id":5,"name":"Paul"}
]
}
}
]
}
}
]
}
Here's something that gets you close to what you want. I structured the output to be an object instead of an array. You don't need the industries or industry properties in your example output. The output structure looks like this:
{
"industry name": {
"id": "id of industry",
"companies": [
{
"company name": "name of company",
"id": "id of company",
"employees": [
{
"id": "id of company",
"name": "name of employee"
}
]
}
]
}
}
I use the _.chain function to wrap the collection with a lodash wrapper object. This enables me to explicitly chain lodash functions.
From there, I use the _.groupBy function to group elements of the collection by their industry name. Since I'm chaining, I don't have to pass in the array again to the function. It's implicitly passed via the lodash wrapper. The second argument of the _.groupBy is the path to the value I want to group elements by. In this case, it's the path to the industry name: company.industry.name. _.groupBy returns an object with each employee grouped by their industry (industries are keys for this object).
I then do use _.transform to transform each industry object. _.transform is essentially _.reduce except that the results returned from the _.transform function is always an object.
The function passed to the _.transform function gets executed against each key/value pair in the object. In the function, I use _.groupBy again to group employees by company. Based off the results of _.groupBy, I map the values to the final structure I want for each employee object.
I then call the _.value function because I want to unwrap the output collection from the lodash wrapper object.
I hope this made sense. If it doesn't, I highly recommend reading Lo-Dash Essentials. After reading the book, I finally got why lodash is so useful.
"use strict";
var _ = require('lodash');
var emps = [
{ "id": 1, "name": "Matthew", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
{ "id": 2, "name": "Mark", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
{ "id": 3, "name": "Luke", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
{ "id": 4, "name": "John", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
{ "id": 5, "name": "Paul", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } }
];
var result = _.chain(emps)
.groupBy("company.industry.name")
.transform(function(result, employees, industry) {
result[industry] = {};
result[industry].id = _.get(employees[0], "company.industry.id");
result[ industry ][ 'companies' ] = _.map(_.groupBy(employees, "company.name"), function( employees, company ) {
return {
company: company,
id: _.get(employees[ 0 ], 'company.id'),
employees: _.map(employees, _.partialRight(_.pick, [ 'id', 'name' ]))
};
});
return result;
})
.value();
Results from your example are as follows:
{
"Medical": {
"id": 5,
"companies": [
{
"company": "abc",
"id": 1,
"employees": [
{
"id": 1,
"name": "Matthew"
},
{
"id": 2,
"name": "Mark"
},
{
"id": 3,
"name": "Luke"
},
{
"id": 4,
"name": "John"
},
{
"id": 5,
"name": "Paul"
}
]
}
]
}
}
If you ever wanted the exact same structure as in the questions, I solved it using the jsonata library:
(
/* lets flatten it out for ease of accessing the properties*/
$step1 := $ ~> | $ |
{
"employee_id": id,
"employee_name": name,
"company_id": company.id,
"company_name": company.name,
"industry_id": company.industry.id,
"industry_name": company.industry.name
},
["company", "id", "name"] |;
/* now the magic begins*/
$step2 := {
"industries":
[($step1{
"industry" & $string(industry_id): ${
"id": $distinct(industry_id)#$I,
"name": $distinct(industry_name),
"companies": [({
"company" & $string(company_id): {
"id": $distinct(company_id),
"name": $distinct(company_name),
"employees": [$.{
"id": $distinct(employee_id),
"name": $distinct(employee_name)
}]
}
} ~> $each(function($v){ {"company": $v} }))]
}
} ~> $each(function($v){ {"industry": $v} }))]
};
)
You can see it in action on the live demo site: https://try.jsonata.org/VvW4uTRz_

dojo How Tree insert data to children?

hi i want to insert data to children to Tree.but I want to put the data.for example i want to update children[0] information.Rather than creating a new one I'd like to update the existing data.
my Tree.json
{
"name": "SCATTER/BUBBLE CHART",
"id": "SCATTERBUBBLE",
"children": [
{
"name": "Series",
"id": "SERIES",
"children": [
{
"name" : "Data:X",
"id" : "DX"
},
{
"name" : "Data:Y",
"id" : "DY"
}
]
},
{
"name": "XAxis",
"id": "X"
},
{
"name": "YAxis",
"id": "Y"
}
]
}
if i click button,i want to result
{
"name": "SCATTER/BUBBLE CHART",
"id": "SCATTERBUBBLE",
"children": [
{
"name": "Series",
"id": "SERIES",
"children": [
{
"name" : "Data:X",
"id" : "DX"
},
{
"name" : "Data:Y",
"id" : "DY"
},
{
"name" : "Data:Z",
"id" : "DZ"
}
]
},
{
"name": "XAxis",
"id": "X"
},
{
"name": "YAxis",
"id": "Y"
},
{
"name": "ZAxis",
"id": "Z"
}
]
}
i don't know update children tree ask for advice
Use node.item to get the store item object which has created the node. I hope you have the node object. For instance if you want to get the root node of your tree :-
var rootNode = dijit.byId("treeID").attr("rootNode");
After you get the node's item object you may update any of its attributes and your store will be modified. Your store should also extend "dojo/store/Observable", so that your tree gets updated with the changes to store.