Join Strings in Query output - sql

I have this query that "i'd like to convert to MongoDB. Is it possible to do this without resorting to javascript?
SELECT FirstName, Lastname, FirstName + " " + LastName AS FullName FROM Users
Also, I am using the most recent version of MongoDB

The only things that can "alter" the form of a returned document are .aggregate() and .mapReduce(). In this case, .aggregate() is the better form with $project and the $concat operator:
db.users.aggregate([
{ "$project": {
"FirstName": 1,
"Lastname": 1,
"FullName": { "$concat": [ "$FirstName", " ", "$Lastname" ] }
}}
])

Related

I have a table with column contains a request from an API that contains special characters and it looks like

{
"Info": {
"code": "SPPACK"
},
"user": {
"firstName": "John",
"lastName": "Smith",
"login": {
"loginType": "MOBILE_NUMBER",
"userName": "91817343123"
}
}
}
I want to retrieve only LoginType and Username values from the JSON above such as
LoginType
Username
MOBILE_NUMBER
91817343123
How can I write a query in oracle to retrieve these values? Please help.
You can use JSON_TABLE() function such as
SELECT LoginType, userName
FROM t,
JSON_TABLE(jstr, '$'
COLUMNS (NESTED PATH '$."user"[*]."login"[*]'
COLUMNS (
LoginType VARCHAR2 PATH '$."loginType"',
userName VARCHAR2 PATH '$."userName"')
))
Demo

I want to combine json rows in t-sql into single json row

I have a table
id
json
1
{"url":"url2"}
2
{"url":"url2"}
I want to combine these into a single statement where the output is :
{
"graphs": [
{
"id": "1",
"json": [
{
"url": "url1"
}
]
},
{
"id": "2",
"json": [
{
"url": "url2"
}
]
}
]
}
I am using T-SQL, I've notice there is some stuff in postgres but can't find much on tsql.
Any help would be greatly appreciated..
You need to use JSON_QUERY on the json column to ensure it is not escaped.
SELECT
id,
JSON_QUERY('[' + json + ']')
FROM YourTable t
FOR JSON PATH, ROOT('graphs');
db<>fiddle

How to get an element in json array of objects db2

DB2 - I'm trying to get an specific item of my json array of objects...
Given this value saved in my column user_properites:
{
"properties":[
{ "key": "firstName", "value": "Jon" },
{ "key": "lastName", "value": "Doe" },
{ "key": "age", "value": 20 }
]
}
I'm using JSON_QUERY and I was doing something like:
SELECT
JSON_QUERY(u.user_properties, '$.properties[1]') AS lastName
FROM
USER u
But I need to get the lastName without pass the position on the array...
Is there a way to search an item by some value?
Thanks for the support :)

How to search Host Groups in Zabbix API

I want to list all Host Groups that match some search criteria.
I've tried that:
data = '{"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"search": {
"name": [
"' + group_name + '"
]
},
},
"id":' + str(msg_id) + ',
"auth": "' + auth + '"
}'
But that is not a correct syntax.
I also tried this:
data = '{"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"' + group_name + '"
]
},
},
"id":' + str(msg_id) + ',
"auth": "' + auth + '"
}'
This one works, but it only matches exactly the group name. And, so, it always returns 1 or 0 matches.
I tried adding the "options":"searchWildcardsEnabled" option in this last query, but it didn't make a difference in the result (i.e. it didn't produce multiple groups as output).
I've found the correct way. I'll post it here in case anyone else needs it later.
data = '{"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"search": {
"name": [
"' + group_name + '"
]
}
},
"id":' + str(msg_id) + ',
"auth": "' + auth + '"
}'
You don't need to specify the wildcard, it's default. Also, you don't need to put the % inside your query.

MongoDB Query Combined and & or

This SQL query:
SELECT name
FROM user
WHERE user.provider = "google" or user.email="email#example.com"
have equivalent mongodb query:
db.user.find({
"$or": [{
"user.provider": "google"
}, {
"user.email": "email#example.com"
}]
}, {
"name": 1
});
How about this one?
SELECT name
FROM user
WHERE (user.provider = "google" and user.id="1") or user.email="email#example.com"
What is the equivalent of the above SQL to mongo? Is it possible to have combined and & or in mongo?
Yes, you can combine them. You should be able to do:
"$or":[
{"$and": [{"user.provider": "google"}, {"user.id": "1"}] },
{ "user.email" : "email#example.com" }
]