How do I search in redis json - redis

I'm currently new to ioredis and I was wondering how o I can search all the json object that has a key value of something.
Example
rooms:{
roomId1: {
name: "room1",
users: [{userId: 1, name: "bob"}, {userId: 2, name: "joe}]
},
roomId2: {
name: "room2",
users: [{userId: 1, name: "jill"}, {userId: 2, name: "joe}]
},
roomId3: {
name: "room3",
users: [{userId: 6, name: "hoi"}, {userId: 1, name: "bob}]
}
}
and I want find all the rooms that has the user "bob" so I want the output to be
[roomId1: {
name: "room1",
users: [{userId: 1, name: "bob"}, {userId: 2, name: "joe}]
}, roomId3: {
name: "room3",
users: [{userId: 6, name: "hoi"}, {userId: 1, name: "bob}]
}]
How cna i achievee this? I guess I can do something like this
const redis = new Redis();
const roomStates = await redis.get("rooms");
//for loop and inner for loop to find the users name bob
but I was wondeirng if theres a faster way of doing it using the redis built in functions

Related

How to compare two arrays in Standard SQL (BigQuery)?

I have two tables with the exact same schema, one of the fields is a record and I try to compare this field between the two tables.
For example:
Table A:
{ Name: 'Mary',
DOB: '06.06.1970',
Children:[
{ Name: John, Age: 6 },
{ Name: Agatha, Age: 10}
]
},
{ Name: 'Bob',
DOB: '30.03.1982',
Children:[
{ Name: Anthony, Age: 9 },
{ Name: William, Age: 4 },
{ Name: Rose, Age: 2 }
]
},
{ Name: 'Adam',
DOB: '312.10.1980',
Children:[
{ Name: Andrew, Age: 9 },
{ Name: Kate, Age: 4 }
]
}
Table B:
{ Name: 'Mary',
DOB: '06.06.1970',
Children:[
{ Name: John, Age: 6 },
{ Name: Agatha, Age: 10}
]
},
{ Name: 'Bob',
DOB: '30.03.1982',
Children:[
{ Name: Anthony, Age: 9 },
{ Name: George, Age: 4 },
{ Name: Frank, Age: 2 }
]
},
{ Name: 'Adam',
DOB: '312.10.1980',
Children:[
{ Name: Andrew, Age: 9 },
{ Name: Kate, Age: 4 },
{ Name: Jonathan, Age: 2 }
]
}
The output I'd like to get is:
Name
----------
Bob
Adam
Since the Children details are not the same for Bob or Adam in table A and table B (whenever it's the size of the record (num of children) or the details (children names etc...))
I tried to use SELECT UNION DISTINCT but it doesn't work on an array, I also tried != and <> but it doesn't work on arrays...
It would be ideal to compare two arrays like you can compare two lists in Python (comparing the structure and the content).
Is there a way to implement something similar ?
Thank you
select name
from `project.dataset.tableA` a
join `project.dataset.tableB` b
using(name)
where a.dob != b.dob
or (select string_agg(format('%t', s) order by name) from a.children s)
!= (select string_agg(format('%t', s) order by name) from b.children s)

How do I perform a SQL update on a list inside one of my table rows?

I have a table with parameters as shown below.
{
id: 1,
title: 'waccos',
description: 'Making an operating system in wacc',
est_duration: 420,
exp_req: 'any',
langs: [ 1 ],
owner: 1,
members: [ 1, 2 ]
},
I want to use SQL to add a new member into the member list, but I do not know how to. The table is called testprojects. I've tried using
(update testprojects
set members = members || $1
where title is $2;', [name, title])
but I think that is wrong. Could anyone help me please?
This is the table of members so far.
[
{ id: 1, name: 'A' },
{ id: 2, name: 'K' },
{ id: 3, name: 'S' },
{ id: 5, name: 'd' },
{ id: 6, name: 'J' },
{ id: 7, name: 'E' },
{ id: 8, name: 'a' }
]

Is there a to set index of a data array within this.setState?

so lets say i have the following data
data: [
{ key: 1, id: 1, uri: "", image:false },
{ key: 2, id: 2, uri: "", image:false },
{ key: 3, id: 3, uri: "", image:false },
{ key: 4, id: 4, uri: "", image:false }
]
I want to update the uri using setState()and so far I have come up with the following using React.
`data: update(this.state.data, {1: {uri: {$set: result.uri}, image:{$set: true}}})`
however, instead of just having 1, I want to be able to pass in index, i know i can do this with if statements but im sure theres an easier and cleaner way??
You should make a copy of data, update it and then setState. For example:
copy
let newData = { ...this.state.data }
update
newData[0].uri = '';
newData[0].image = true
setState
this.setState({data: newData})

LokiJS: insert existing value for index doesn't error - how to make unique indices?

if I try to override an existing indexed field, I do not get an error.
It should error, because it is not update()!
var loki = require('lokijs');
var db = new loki('test.json');
var users = db.addCollection('users', { indices: ['userID']});
users.insert(
{
'name': 'Anna',
'userID': 1
},
{
'name': 'Bernd',
'userID': 2
},
{
'name': 'Christa',
'userID': 3
});
db.save();
users.insert({'name': 'Dieter','userID': 2}); // this should error!!
How can I make an unique index to get an error when trying to inset an existing userID ?
the indices option creates an index on the field, which allows for faster retrieval because the index lives in a separate sorted array within the collection (so Loki can use binary-search instead of a full loop to fetch records). However, you're looking for a unique index, which is created with ensureUniqueIndex (check here, scroll down to Finding Documents, there's a section on unique indexes.). With that, you can use the collection method by(field, value) (which can even be curried if you only pass the field value), which uses the unique index to full potential (about 2x the speed of an indexed field). Remember that you need to explicitly call ensureUniqueIndex because unique indexes cannot be serialized and persisted.
update: once the ensureUniqueIndex method is called, the collection will throw an error if you try to insert a duplicate key record. If you have repository checked out you can take a look at spec/generic/unique.spec.js for an example ( here )
var loki = require('lokijs');
var db = new loki('test.json');
var users = db.addCollection('users', { indices: ['userID']});
users.ensureUniqueIndex('userID');
users.on('error',function(obj){
console.log('error ... adding 1 to userID');
obj.userID = obj.userID+1;
return obj;
});
users.insert(
{
'name': 'Anna',
'userID': 1
});
users.insert(
{
'name': 'Bernd',
'userID': 2
});
users.insert(
{
'name': 'Christa',
'userID': 3
});
db.save();
console.log(users.data);
try {
users.insert({'name': 'Dieter','userID': 2}); // this should error!!
} catch(e){
var i = 2+1;
users.insert({'name': 'Dieter','userID': i}); // this should error!!
}
db.save();
db2 = new loki('test.json');
db2.loadDatabase({}, function () {
var users2 = db2.getCollection('users')
console.log(users2.data);
});
Either users.on('error',...) nor try{ users.insert...} catch(e){// id+1} handles the thrown error
That's my console:
[ { name: 'Anna',
userID: 1,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 1 },
{ name: 'Bernd',
userID: 2,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 2 },
{ name: 'Christa',
userID: 3,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 3 } ]
Duplicate key for property userID: 2
[ { name: 'Anna',
userID: 1,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 1 },
{ name: 'Bernd',
userID: 2,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 2 },
{ name: 'Christa',
userID: 3,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 3 },
{ name: 'Dieter',
userID: 2,
meta: { revision: 0, created: 0, version: 0 },
'$loki': 4 } ]

Creating sorted tree in DOJO 1.6?

I new to learn dojo and trying to learn by it using samples code.
Using dojo 1.6
With help of sample codes , I created a tree
now i want to apply sorting on root and also on child.
With the help of this sample code , i changed the code
Output is not sorted n but the root folder has changed their position and child is deleted.
Plz help me to resolve this.
My code :
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.tree.ForestStoreModel");
dojo.require("dijit.Tree");
var data = [ { id: 1, name: "answerTypeLabel", type:'scenario', children:[{_reference: 2}]},
{ id: 2, name: "acceptRequestLabel", type:'paragraph', data: "acceptRequestLabel"},
{ id: 3, name: "rejectRequestLabel", type:'scenario', children:[{_reference: 5},{_reference: 6}]},
{ id: 4, name: "MoreInformationLabel", type:'scenario', children:[{_reference: 7},{_reference: 8}]},
{ id: 5, name: "rejectRequestStatusLabel", type:'paragraph', data: "rejectRequestStatusLabel"},
{ id: 6, name: "rejectRequestNotCoveredLabel", type:'paragraph', data: "rejectRequestNotCoveredLabel" },
{ id: 7, name: "MoreInformationDocumentLabel", type:'paragraph', data: "MoreInformationDocumentLabel"},
{ id: 8, name: "MoreInformationDataLabel", type:'paragraph', data: "MoreInformationDataLabel"}
];
dojo.addOnLoad(function() {
var sortableStore = new dojo.data.ItemFileReadStore({
data: {
identifier: 'id',
label: 'name',
items: data
}
});
var model = new dijit.tree.ForestStoreModel({
rootLabel: 'Names',
store: new dojo.data.ItemFileWriteStore({
data: {
identifier: 'id',
items: [],
label: 'name'
}
}) // blank itemsstore
})
var tree = new dijit.Tree({
model: model,
updateItems: function(items) {
var self = this;
console.log('pre', this.model.root.children);
dojo.forEach(items, function(newItem) {
console.log('add', newItem);
try {
self.model.store.newItem({
id: sortableStore.getValue(newItem, 'id'),
name: sortableStore.getValue(newItem, 'name'),
type: sortableStore.getValue(newItem, 'type'),
data: sortableStore.getValue(newItem, 'data'),
});
} catch (e) {
console.log(e);
}
});
console.log('post', this.model.root.children);
console.log("children: ", this.rootNode.getChildren());
},
});
tree.placeAt(dojo.body());
sortableStore.fetch({
query: {
type:'scenario'
},
sort: [{
attribute: "name"}],
onComplete: function(items) {
console.log(items, 'sorted');
tree.updateItems(items);
}
})
});
Output :
The 'Names' origins from you setting 'rootLabel'.
Btw, fiddles have revisions and is simply a paste-bin like feature :)
You need to use the tree model pasteItem to insert referenced items (the 'children' property of each 'newItem').
Otherwise, there's another approach, if you get rid of the '_reference' structure of your data. See: http://jsfiddle.net/GHFdA/1/