Leaderboard from database using MongoDB/Mongoose - javascript

In each document there is a level field with a Number value and a twitch_username with a String value. I want to return these values like so:
1. Twitch: <twitch_username> Level: <level>
2. Twitch: <twitch_username> Level: <level>
3. Twitch: <twitch_username> Level: <level>
//...
I tried mapping through the data, running a for loop, pushing the data into a new array, etc. but can't seem to wrap my head around it.
Currently running this query which returns the specific data I need in descending order:
User.find({}).select('twitch_username level -_id').sort({level: 'desc'}).exec((err,docs) => {
// console.log(docs);
});
The query above returns:
[
{ twitch_username: 'user1', level: 67 },
{ twitch_username: 'user2', level: 14 }
]
Which is sort of what I want but I can't individually label each value, the data is returned but I can't do anything with it.

You can use array reduce to reduce it to a string
const output = [
{ twitch_username: 'user1', level: 67 },
{ twitch_username: 'user2', level: 14 }
];
Then use reduce function
output.reduce((final, current, id) => {
return (final + (id+1) + '. Twitch: ' + current.twitch_username + ' Level: ' + current.level + '\n');
}, '')

Related

how to loop through multiple arrays inside an array and filter a value from it-Javascript

I'm using EXTJS framework for my code.
below is my array structure:
data = [{
id: 22,
rows: [{
id: "673627",
name: "ABS",
address: "536street"
}, {
id: "333",
name: "TEST$$",
address: "536street"
}, {
id: "999",
name: "TEST$$",
address: "536street"
}]
}, {
id: 33,
rows: [{
id: "899",
name: "TES",
address: "536street"
}, {
id: "333",
name: "TEST$$",
address: "536street"
}, {
id: "999",
name: "TES673",
address: "536street"
}]
}]
Now I want to filter the name from this array, whose value I'm comparing with say "TEST$$".
I'm doing this;
Ext.each(data, function(item) {
filter = item.rows.filter(function(name) {
return name.name === "TEST$$";
}, this);
}, this);
console.log(filter);
In this case, it returns only 1 match, where as I have 3 matches for this particular value. It returns the match from the last item in the data array and hence I dont get all the matching values, any idea how this can be looped to get all values matching?
thx!
You're reassigning the filter variable on every iteration over the data array:
filter = item.rows.filter(function(name) {
return name.name === "TEST$$";
}, this);
On the last iteration, there is only one match, the one with id of 333, so that's the only one that you see after running the Ext.each. Try pushing to an external array that doesn't get overwritten instead:
const testItems = [];
Ext.each(data, function(item) {
const filtered = item.rows.filter(row => row.name === "TEST$$")
testItems.push(...filtered);
});
console.log(testItems);
Note that there's no need to pass along the this context.
Another option is to flatMap to extract all rows to a single array first:
const output = data
.flatMap(({ rows }) => rows)
.filter(({ name }) => name === 'TEST$$');

javascript remove using reduce

I'm trying to just get particular values in an obj with the status of 'xyz'
user.friends =
CoreMongooseArray [
{ _id: 5b11824350c0011afe9ca310,
user:
{ profile: [Object],
_id: 5b11805a50c0011afe9ca2fe,
username: 'user1' },
status: 'abc' },
{ _id: 5b191033d240ab4a10ffb54f,
user:
{ profile: [Object],
_id: 5b0ec81f958f5b4919b83c40,
username: 'user2' },
status: 'xyz' } ]
I'm using
user.friends.reduce((a, t) => a + (t.type === 'xyz' ? 0 : 1), 0);
but it only returns 1, rather than just the object with the username user2, why?
but it only returns 1, rather than just the object with the username user2, why?
Because that's the last return value of your callback. This is how reduce works.
If you want to find one particular object in an array matching a criterion, reduce isn't the tool to use (though you could force it to work, just as you can use a screwdriver as a hammer if you try hard enough). The tool to use is find:
const oneXYZFriend = user.friends.find(e => e.status === 'xyz');
(Notice I changed the test from e.type === 'xyz' to e.status === 'xyz', since your objects have a status property, not a type property.)
If you want to find several objects in an array matching a criterion, you'd use filter:
const xyzFriends = user.friends.filter(e => e.status === 'xyz');

JavaScript : searching an array and returning value after user input

var data = [{
id: 'A1',
name: 'Minstrels'
}, {
id: 'A2',
name: 'Bounty'
}, {
id: 3,
name: 'Crunchie'
}, {
id: 4,
name: 'bar'
}];
var rl = require('readline')
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question('Which Product do you want to purchase? ',
function(Answer1) {
//missing code goes here in order to return Minstrels if the user
//types 'A1' etc..
}
)
I need help in my code. I have enabled user input and made an array, however, I can't think of what my code is missing.
This uses the find() array method to locate the first element matching the given predicate, then additionally handles the case where the item was not located. Because your example uses both strings and numbers for id values, we explicitly coerce each object's id to a string during comparison to simplify the strict comparison:
var item = data.find(i => ("" + i.id) === Answer1);
console.log(item ? item.name : 'No such food');

How to sort using ng-repeat with a key|value pair

I'm attempting to list out some messaging threads for my application in the order of the last message (So that newer messages are on the top) and I'm having some problems using the orderBy filter that comes with Angular.
Here's an example of what the object we're sorting looks like.
$scope.messageThreads = {
'102842': {
name: 'John Doe',
avatar: 'http://directlink/img.png',
last_message: 5,
messages: [
{ content: 'Hello!', from: '102842', to: '312124', date: 5 }
]
},
'59251275': {
name: 'Thomas Doe',
avatar: 'http://directlink/img.png',
last_message: 28,
messages: [
{ content: 'Hey jack', from: '59251275', to: '1231251', date: 12 },
{ content: 'Hey Thomas', from: '1231251', to: '59251275', date: 28 }
]
}
}
Stored in a simple object called messageThreads.
In the HTML document, we are looping over these in a Key/Value pair, as so:
<div ng-repeat="(id, thread) in messageThreads | orderBy: '-thread.last_message'">
Thread ID: {{id}} - Last message: {{thread.last_message}}
</div>
Unfortunately, it's not ordering the messages at all. The output displayed is:
Thread ID: 102842 - Last message: 5
Thread ID: 59251275 - Last message: 28
However, it should be:
Thread ID: 59251275 - Last message: 28
Thread ID: 102842 - Last message: 5
Changing -thread.last_message to thread.last_message does not fix the issue.
Here is solution but one problem with that is it converts the items object to an array, you will no longer have access to the key of the “associative array”, as you do with the (key, item) in items snippet
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item,key) {
**item.key = key;** //Here you can push your key to get in returned Object
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
In HTML:
<div ng-repeat="(id, thread) in messageThreads | orderObjectBy:'last_message':true">
Thread ID: {{thread.key}} - Last message: {{thread.last_message}}
</div>

RethinkDB - Updating nested array

I have a survey table that looks like so:
{
id: Id,
date: Date,
clients: [{
client_id: Id,
contacts: [{
contact_id: Id,
score: Number,
feedback: String,
email: String
}]
}]
}
I need to updated the score and feedback fields under a specific contact. Currently, I am running the update like this:
function saveScore(obj){
var dfd = q.defer();
var survey = surveys.get(obj.survey_id);
survey
.pluck({ clients: 'contacts' })
.run()
.then(results => {
results.clients.forEach((item, outerIndex) => {
item.contacts.forEach((item, index, array) => {
if(Number(item.contact_id) === Number(obj.contact_id)) {
array[index].score = obj.score;
console.log(outerIndex, index);
}
});
});
return survey.update(results).run()
})
.then(results => dfd.resolve(results))
.catch(err => dfd.resolve(err));
return dfd.promise;
};
When I look at the update method, it specifies how to update nested key:value pairs. However, I can't find any examples to update an individual item in an array.
Is there a better and hopefully cleaner way to update items in a nested array?
You might need to get the array, filter out the desired value in the array and then append it again to the array. Then you can pass the updated array to the update method.
Example
Let's say you have a document with two clients that both have a name and a score and you want to update the score in one of them:
{
"clients": [
{
"name": "jacob" ,
"score": 200
} ,
{
"name": "jorge" ,
"score": 57
}
] ,
"id": "70589f08-284c-495a-b089-005812ec589f"
}
You can get that specific document, run the update command with an annonymous function and then pass in the new, updated array into the clients property.
r.table('jacob').get("70589f08-284c-495a-b089-005812ec589f")
.update(function (row) {
return {
// Get all the clients, expect the one we want to update
clients: row('clients').filter(function (client) {
return client('name').ne('jorge')
})
// Append a new client, with the update information
.append({ name: 'jorge', score: 57 })
};
});
I do think this is a bit cumbersome and there's probably a nicer, more elegant way of doing this, but this should solve your problem.
Database Schema
Maybe it's worth it to create a contacts table for all your contacts and then do a some sort of join on you data. Then your contacts property in your clients array would look something like:
{
id: Id,
date: Date,
clients: [{
client_id: Id,
contact_scores: {
Id: score(Number)
},
contact_feedbacks: {
Id: feedback(String)
}
}]
}
database schema
{
"clients": [
{
"name": "jacob" ,
"score": 200
} ,
{
"name": "jorge" ,
"score": 57
}
] ,
"id": "70589f08-284c-495a-b089-005812ec589f"
}
then you can do like this using map and branch query .
r.db('users').table('participants').get('70589f08-284c-495a-b089-005812ec589f')
.update({"clients": r.row('clients').map(function(elem){
return r.branch(
elem('name').eq("jacob"),
elem.merge({ "score": 100 }),
elem)})
})
it works for me
r.table(...).get(...).update({
contacts: r.row('Contacts').changeAt(0,
r.row('Contacts').nth(0).merge({feedback: "NICE"}))
})
ReQL solution
Creating a query to update a JSON array of objects in-place, is a rather complicated process in ReThinkDB (and most query languages). The best (and only) solution in ReQL that I know about, is to use a combination of update,offsetsOf,do,changeAt, and merge functions. This solution will retain the order of objects in the array, and only modify values on objects which match in the offsetsOf methods.
The following code (or something similar) can be used to update an array of objects (i.e. clients) which contain an array of objects (i.e. contracts).
Where '%_databaseName_%', '%_tableName_%', '%_documentUUID_%', %_clientValue_%, and %_contractValue_% must be provided.
r.db('%_databaseName_%').table('%_tableName_%').get('%_documentUUID_%').update(row =>
row('clients')
.offsetsOf(clients => client('client_id').eq('%_clientValue_%'))(0)
.do(clientIndex => ({
clients: row('clients')(clientIndex)
.offsetsOf(contacts => contact('contact_id').eq('%_contactValue_%')))(0)
.do(contactIndex => ({
contacts: row(clientIndex)
.changeAt(contractIndex, row(clientIndex)(contractIndex).merge({
'score': 0,
'feedback': 'xyz'
}))
})
}))
)
Why go through the trouble of forming this into ReQL?
survey
.pluck({ clients: 'contacts' }).run()
.then(results => {
results.clients.forEach((item, outerIndex) => {
item.contacts.forEach((item, index, array) => {
if(Number(item.contact_id) === Number(obj.contact_id)) {
array[index].score = obj.score;
console.log(outerIndex, index);
}
});
});
return survey.update(results).run()
})
While the code provided by Jacob (the user who asked the question here on Stack Overflow - shown above) might look simpler to write, the performance is probably not as good as the ReQL solution.
1) The ReQL solution runs on the query-server (i.e. database side) and therefore the code is optimized during the database write (higher performance). Whereas the code above, does not make full use of the query-server, and makes a read and write request pluck().run() and update().run(), and data is processed on the client-request side (i.e. NodeJs side) after the pluck() query is run (lower performance).
2) The above code requires the query-server to send back all the data to the client-request side (i.e. NodeJs side) and therefore the response payload (internet bandwidth usage / download size) can be several megabytes. Whereas the ReQL solution is processed on the query-server, and therefore the response payload typically just confirms that the write was completed, in other words only a few bytes are sent back to the client-request side. Which is done in a single request.
ReQL is too complicated
However, ReQL (and especially SQL) seem overly complicated when working with JSON, and it seems to me that JSON should be used when working with JSON.
I've also proposed that the ReThinkDB community adopt an alternative to ReQL that uses JSON instead (https://github.com/rethinkdb/rethinkdb/issues/6736).
The solution to updating nested JSON arrays should be as simple as...
r('database.table').update({
clients: [{
client_id: 0,
contacts: [{
contact_id: 0,
score: 0,
feedback: 'xyz',
}]
}]
});
tfmontague is on the right path but I think his answer can be improved a lot. Because he uses ...(0) there's a possibility for his answer to throw errors.
zabusa also provides a ReQL solution using map and branch but doesn't show the complete nested update. I will expand on this technique.
ReQL expressions are composable so we can isolate complexity and avoid repetition. This keeps the code flat and clean.
First write a simple function mapIf
const mapIf = (rexpr, test, f) =>
rexpr.map(x => r.branch(test(x), f(x), x));
Now we can write the simplified updateClientContact function
const updateClientContact = (doc, clientId, contactId, patch) =>
doc.merge
( { clients:
mapIf
( doc('clients')
, c => c('client_id').eq(clientId)
, c =>
mapIf
( c('contacts')
, c => c('contact_id').eq(contactId)
, c =>
c.merge(patch)
)
)
}
);
Use it like this
// fetch the document to update
const someDoc =
r.db(...).table(...).get(...);
// create patch for client id [1] and contact id [12]
const patch =
updateClientContact(someDoc, 1, 12, { name: 'x', feedback: 'z' });
// apply the patch
someDoc.update(patch);
Here's a concrete example you can run in reql> ...
const testDoc =
{ clients:
[ { client_id: 1
, contacts:
[ { contact_id: 11, name: 'a' }
, { contact_id: 12, name: 'b' }
, { contact_id: 13, name: 'c' }
]
}
, { client_id: 2
, contacts:
[ { contact_id: 21, name: 'd' }
, { contact_id: 22, name: 'e' }
, { contact_id: 23, name: 'f' }
]
}
, { client_id: 3
, contacts:
[ { contact_id: 31, name: 'g' }
, { contact_id: 32, name: 'h' }
, { contact_id: 33, name: 'i' }
]
}
]
};
updateClientContact(r.expr(testDoc), 2, 23, { name: 'x', feedback: 'z' });
The result will be
{ clients:
[ { client_id: 1
, contacts:
[ { contact_id: 11, name: 'a' }
, { contact_id: 12, name: 'b' }
, { contact_id: 13, name: 'c' }
]
}
, { client_id: 2
, contacts:
[ { contact_id: 21, name: 'd' }
, { contact_id: 22, name: 'e' }
, { contact_id: 23, name: 'x', feedback: 'z' } // <--
]
}
, { client_id: 3
, contacts:
[ { contact_id: 31, name: 'g' }
, { contact_id: 32, name: 'h' }
, { contact_id: 33, name: 'i' }
]
}
]
}
Better late than never
I had your same problem and i could solve it with two ways:
With specific client_id
r.db('nameDB').table('nameTable').get('idRegister')
.update({'clients': r.row('clients')
.map(elem=>{
return r.branch(
elem('client_id').eq('your_specific_client_id'),
elem.merge({
contacts: elem('contacts').map(elem2=>
r.branch(
elem2('contact_id').eq('idContact'),
elem2.merge({
score: 99999,
feedback: 'yourString'
}),
elem2
)
)
}),
elem
)
})
})
Without specific client_id
r.db('nameDB').table('nameTable').get('idRegister')
.update({'clients': r.row('clients')
.map(elem=>
elem.merge({
contacts: elem('contacts').map(elem2=>
r.branch(
elem2('contact_id').eq('idContact'),
elem2.merge({
score: 99999,
feedback: 'yourString'
}),
elem2
)
)
})
)
})
I hope that it works for you, even when happened much time ago

Categories

Resources