N1QL select number-only key - couchbase

i have a document like
{
"bucket_name": {
"0": 2,
"1": 107,
"2": 61,
"3": 428,
"username": "tester2",
"obj_type": 21,
"date": 1562416800
}
}
how i can do select to number-only json key like that, i tried something like :
select 0 from bucket_name where obj_type=21 and username='tester'
but it's return :
[
{
"$1": 0,
},
{
"$1": 0,
},
{
"$1": 0,
},
]

USE back-ticks (i.e. escaped-identifiers) https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/identifiers.html
SELECT `0`
FROM bucket_name
WHERE obj_type=21 AND username='tester';

Related

Calculate min and max from json data and store the result as list

I have data in JSON format, the sample is given below. I want to calculate the min and max range for the values inside the variable i.e. align_X, align_Y, align_Z.
{"id": 0, "variable": {"align_X": 41, "align_Y": 51, "align_Z": 80}}
{"id": 1, "variable": {"align_X": 0}}
{"id": 2, "variable": {"align_Y": 1, "align_Z": 0}}
Desired output is:
"align_X": [
0.0,
41.0
],
"align_Y": [
1.0,
51.0
],
"align_Z": [
0.0,
80.0
]
Any help is appropriated.
Thank you
Can you try this
var test = [{
"id": 0,
"variable": {
"align_X": 41,
"align_Y": 51,
"align_Z": 80
}
}, {
"id": 1,
"variable": {
"align_X": 0
}
}, {
"id": 2,
"variable": {
"align_Y": 1,
"align_Z": 0
}
}
]
var result = test.reduce((acc, cobj) => {
for (key in cobj.variable) {
if (acc[key]) {
if(acc[key][0] > cobj.variable[key]){
if(!acc[key][1]){
acc[key][1] = acc[key][0];
}
acc[key][0] = parseFloat(cobj.variable[key]).toFixed(2)
} else if(acc[key][1] == undefined || acc[key][1] < cobj.variable[key]){
acc[key][1] = parseFloat(cobj.variable[key]).toFixed(2)
}
}
else {
acc[key] = [];
acc[key].push(parseFloat(cobj.variable[key]).toFixed(2))
}
}
return acc;
}, []);
console.log(result);
My solution
#Your input
d = [{"id": 0, "variable": {"align_X": 41, "align_Y": 51, "align_Z": 80}},
{"id": 1, "variable": {"align_X": 0}},
{"id": 2, "variable": {"align_Y": 1, "align_Z": 0}}
]
#To store result float(-inf) smallest number in python & float(inf) is largest
output = {
"align_X" : [float('inf'),float('-inf')],
"align_Y" : [float('inf'),float('-inf')],
"align_Z" : [float('inf'),float('-inf')]
}
for item in d:
for each in output.keys():
try:
if item['variable'][each]<output[each][0]:
output[each][0] = item['variable'][each]
if item['variable'][each]>output[each][1]:
output[each][1] = item['variable'][each]
except:
continue
print(output)

delete object from nested document in mongodb

I have this JSON above with _id, date, and two objects: sessionData and metaData. I want to delete from every document the object metaData where "userId":123456
{
"_id": {
"$oid": "60feedd4b3aefff2629b93b7"
},
"insertionDate": {
"$date": "2021-07-26T18:15:57.564Z"
},
"sessionData": {
"time": [1364, 1374, 1384],
"yaw": [0.15, 0.3, 0.45],
"pitch": [0.36, 0.76, 1.08],
"roll": [-0.13, -0.25, -0.35],
"heading": [-3.24, -3.25, -3.17],
"ax": [-0.42, -0.41, -0.41],
"ay": [-0.15, -0.13, -0.1],
"az": [0.9, 0.91, 1],
"gx": [0, 0, 0],
"gy": [-0.01, 0, -0.01],
"gz": [0.02, 0.02, 0.02],
"mx": [0.26, 0.26, 0.26],
"my": [0.01, 0.01, 0.01],
"mz": [-0.04, -0.04, -0.07]
},
"metaData": {
"userId": 123456,
"gender": "M",
"ageGroup": "SENIOR",
"weightKg": 70,
"heightCm": 175,
"poolSizeM": 50
}
}
The code I have:
#app.route('/data/anonymousData/<int:l>', methods = ['POST'])
def makeanonymous(l):
result = collection.update_many({}, {"$pull":{ "metaData": {"$in": {"userId": l }}}}, multi=True )
t=result.deleted_count
return f'Deleted {t} documents.'
You can use $unset to remove the object like this:
collection.update_many({
"metaData.userId": 123456
},
{
"$unset": {
"metaData": ""
}
})
Check this example.

jq - how to format pairs of keys and values, then iterate keys in json

Please help me, guys.
I have the following:
{
"id": 88888,
"name": "TESTING",
"online": 1,
"test_list": [
{
"id": 0,
"type": 34,
"name": "Blabla",
"used": 9870,
"rate": 31768897891,
"temp": 0,
"percent": 100,
"enabled": 0,
"reset": 0,
"temp": 2
},
{
"id": 1,
"type": 23,
"name": "AlbAlb",
"used": 0865,
"rate": 317681675432345678951,
"temp": 1,
"percent": 99,
"enabled": 0,
"reset": 0,
"target": 2
},
{
"id": 2,
"type": 98,
"name": "TztS",
"used": 65,
"rate": 6324,
"temp": 0,
"percent": 80,
"enabled": 0,
"reset": 0,
"target": 2
}
],
"info": {
"version": "1.0",
"no": "123",
"sw": "321",
"version_detail": {
"date": "2021-03-30",
"build": "11",
"cot": "dfgdfgd"
}
}
}
And i need to do the following:
[
{
"name": "TESTING",
"online": 1
},
{
"id0": 0,
"type0": 34,
"name0": "Blabla",
"rate0": 31768897891,
"temp0": 0,
"percent0": 100
"id1": 1,
"type1": 23,
"name1": "AlbAlb",
"rate1": 317681675432345670000,
"temp1": 1,
"percent1": 99
"id2": 2,
"type2": 98,
"name2": "TztS",
"rate2": 6324,
"temp2": 0,
"percent2": 80
}
]
I want to join the information, what I need into blocks and then set the key numbering by id or iterate.
Will it be possible to somehow realised it using jq and man in one line?
After a long time, I managed to do only the following:
jq '[. | {name,online}] + [.test_list[] | {id,type,name,rate,temp,percent} | with_entries(.key |= . + "0")]'
I tried using map tool, but it doesn't work = (( Help Please...
Here's a reduce-free approach: it uses with_entries on an array to do the counting:
[ {name, online},
(.test_list
| with_entries(.key |= tostring
| .key as $k
| .value |= with_entries(.key += $k))
| add
)
]
The key is with_entries. One possibility would be to use reduce:
[ {name, online},
(.test_list
| . as $in
| reduce range(0; length) as $i ({}; . + ($in[$i] | with_entries(.key = "\(.key)\($i)")))
)
]
Please note that your example output is not quite correct as JSON, and that with your example input, the above jq program produces slightly different results, but if you want to remove certain key-value pairs, you can easily do that, e.g. as a preliminary step in the pipeline.

unable to add value on nested property

I am struggling to modify the data of properties that are deeply nested in my PSObject. If I try to modify the values Powershell just throws the exception of saying that it cannot find the value, but if I just call upon the value itself it can be found without an issue.
The raw data of the json object looks like this:
[
{
"#CREATED": "2017-05-04 08:36:23",
"#LAST_MODIFIED": "2017-05-08 08:18:58",
"STANDARD_FIELDS": {
"FIRST_NAME": {
"#FIELD_ID": 2,
"#LAST_MODIFIED": "2017-05-04 08:36:24",
"#VALID_FROM": "2017-05-04",
"value": "Example"
},
"LAST_NAME": {
"#FIELD_ID": 3,
"#LAST_MODIFIED": "2017-05-04 08:36:24",
"#VALID_FROM": "2017-05-04",
"value": "User"
},
"ORGANIZATION": {
"#FIELD_ID": 8,
"#MODE": 0,
"#LAST_MODIFIED": "2017-05-04 08:36:24",
"#VALID_FROM": "2017-05-04",
"value": "Example Org"
},
"NAME": {
"#FIELD_ID": 1,
"#MODE": 1,
"#LAST_MODIFIED": "2017-05-04 08:36:24",
"#VALID_FROM": "2017-05-04",
"#ID": 240
},
"SUPERVISOR": {
"#FIELD_ID": 9,
"#MODE": 4,
"#LAST_MODIFIED": "2017-05-04 15:39:30",
"#VALID_FROM": "2017-05-04",
"NAME": "Example User2",
"SYSTEM_ID": "198"
},
"JOB_LOCATION": {
"#FIELD_ID": 14,
"#MODE": 0,
"#LAST_MODIFIED": "2017-05-04 14:26:55",
"#VALID_FROM": "2017-05-04",
"value": "Stockholm"
}
},
"CUSTOM_FIELDS": {
"FIELD": [
{
"#FIELD_ID": 1033,
"#MODE": 0,
"#LAST_MODIFIED": "2017-05-24 10:15:24",
"#VALID_FROM": "2017-05-24",
"NAME": "Department",
"VALUE": {
"value": "Example Department"
}
},
{
"#FIELD_ID": 1034,
"#MODE": 0,
"#LAST_MODIFIED": "2017-05-04 08:36:24",
"#VALID_FROM": "2017-05-04",
"NAME": "VPN",
"VALUE": {
"value": "Yes"
}
},
{
"#FIELD_ID": 1036,
"#LAST_MODIFIED": "2017-05-04 08:36:24",
"#VALID_FROM": "2017-05-04",
"NAME": "Titel",
"VALUE": {
"value": "Test-User"
}
},
{
"#FIELD_ID": 1022,
"#MODE": 0,
"#LAST_MODIFIED": "2017-05-04 16:43:18",
"#VALID_FROM": "2017-05-04",
"NAME": "Authorizations",
"VALUES": {
"VALUE": [
{
"value": "Auth 1"
},
{
"value": "Auth 2"
},
{
"value": "Auth 3"
},
{
"value": "Auth 4"
}
]
}
}
]
}
}
]
My current code:
$users = get-content "C:\Working-Branch\Temp\NotFoundUsers - Copy.txt"| ConvertFrom-Json
foreach($user in $users){
$user.CUSTOM_FIELDS.FIELD.values.Value.value
$NewValue = $user.CUSTOM_FIELDS.FIELD.values.Value.value
$NewValue += "Test"
$NewValue
$($user.CUSTOM_FIELDS.FIELD.values.Value).value = $newValue
}
Which results in the following output:
Auth 1
Auth 2
Auth 3
Auth 4
Auth 1
Auth 2
Auth 3
Auth 4
Test
The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At line:10 char:5
+ $($user.CUSTOM_FIELDS.FIELD.values.Value).value = $newValue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
So I know the property exists because I call upon it two times, one directly on the Object and one time after I have added the value "test" to it. But when I try to update the Orignal object it says it can not find the value "Value" on the object.
I tried following the what was suggested in This post, but it still fails for me. I both with the "+=" operator and the "=" operator. Same error with both of them.
Result of Get-member
I think you need to find the field with the member Values
$newvalue =#{
"value" = "Test"
}
foreach($user in $users){
$array = $user.CUSTOM_FIELDS.FIELD.values.value
$array += $newvalue
foreach ($field in $user.CUSTOM_FIELDS.FIELD)
{
if ($field.values -ne $null)
{
$field.values.value = $array
}
}
$user.CUSTOM_FIELDS.FIELD.values.Value
}
Then output for $user.CUSTOM_FIELDS.FIELD.values.Value is
value
-----
Auth 1
Auth 2
Auth 3
Auth 4
Test

parse json output for primary and secondary hosts from replSetGetStatus

I've used pymongo to connect to mongo replica set and print the status of replica set using json dump. I want to parse this output and display "name" and "stateStr" into a list or array for the user to be able to pick a particular host.Here is my json dump output:
{
{
"replSetGetStatus": {
"date": "2016-10-07T14:21:25",
"members": [
{
"_id": 0,
"health": 1.0,
"name": "xxxxxxxxxxx:27017",
"optime": null,
"optimeDate": "2016-10-07T13:50:11",
"self": true,
"state": 1,
"stateStr": "PRIMARY",
"uptime": 32521
},
{
"_id": 1,
"health": 1.0,
"lastHeartbeat": "2016-10-07T14:21:24",
"lastHeartbeatRecv": "2016-10-07T14:21:24",
"name": "xxxxxxxxxxxx:27017",
"optime": null,
"optimeDate": "2016-10-07T13:50:11",
"pingMs": 0,
"state": 2,
"stateStr": "SECONDARY",
"syncingTo": "xxxxxxxxxxxx:27017",
"uptime": 27297
},
{
"_id": 2,
"health": 1.0,
"lastHeartbeat": "2016-10-07T14:21:24",
"lastHeartbeatRecv": "2016-10-07T14:21:24",
"name": "xxxxxxxxxxxxx:27020",
"pingMs": 0,
"state": 7,
"stateStr": "ARBITER",
"uptime": 32517
}
],
"myState": 1,
"ok": 1.0,
"set": "replica1"
}
}
Please try below Javascript code. It worked for me.
use admin;
var result = rs.status();
var length = result.members.length;
for (var i=0;i<length;i++){
print ("Server Name-" +result.members[i].name);
print ("Server State-" +result.members[i].stateStr);
}