I know the method to find a model in a collection from a single attribute,
var musketeers = friends.where({job: "Musketeer"});
but if i want contemporary find by two attribute and return a single result?
My idea is to find once a time the single attributes and after connect the two result:
var name = friends.where({name: "stefano"});
var surname = friends.where({surname: "maglione"});
and after connect the results.
I need because i'm coding a search function to find people based on name and surname.
But there is another problem:if user insert before name and after surname or vice versa?
You could also use the filter method to achieve it
var musketeers = friends.filter(function(friend) {
return friend.get('job') === stefano' && friend.get('surname') === 'maglione';
});
Collection.where in fact accepts multiple attributes:
where collection.where(attributes)
Return an array of all the models in a collection that match the passed attributes.
Useful for simple cases of filter.
You could write your request as
var found = friends.where({
name: "stefano",
surname: "maglione"
});
See http://jsfiddle.net/nikoshr/kuLYE/ for a demo
Related
I am trying to find out how to get a user list via their id in tabular form (array).
I've tried this :
let result = message.guild.members.get(id_list)
But it doesn't work ...
id list :
var id_list = [ '223515154229231616',
'447425491041910807',
'479932358422691840',
'464536970417143828',
'299429099611357185',
'332868897646837762',
'198346639843262464',
'266078466972057600',
'334270225895653379',
'198884536363253761',
'216595784286601216',
'358993909269135371',
'248894149162565647',
'396681438688182284',
'349270173011804171',
'358319021726236672',
'216136890963853313',
'282994966014459915',
'298171616968572930',
'257809919036751872',
'239365876846034944',
'502162147942596609',
'449391921840914444',
'473229490584158208' ]
The following should do the trick
let result = id_list.map(id => client.users.get(id));
Basically, you loop through all the ids you have using .map() and fetch the user associated.
You may want to remove all the undefined results (when your bot "does not know" the user linked to a specific id), in that case you need to add the following after the previous piece of code :
result = result.filter(r => r !== undefined);
Check both the documentation of .map() and .filter() for more information
This will not work as it is for one ID at a time, not an array of them. You would need to iterate over the IDs in the array, I suggest using a for loop for this, using id_list.length as one of the parameters. For example:
for(var i = 0; i <= id_list.length; i++) {
//make the request to get the member information
}
Also, just to make sure, you might be better off using client.users.get('id'); because if the user sends a direct message, there is no guild property so an error will be thrown.
You can use
let result = id_list.map(id => client.users.get(id).tag)
That should give back an array with like
['User#0000',
'NextUser#4821'
]
I'm trying to create a compound query to do a range query.
let query;
category.queryParams.query.fieldFilters.forEach(function(fieldFilter) {
query = collection.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
});
But it's not giving me the results I want.
I'm having a feeling that the query needs to be created chained like collection.where().where()
If that is the case, how can I transform the contents of my array using that syntax?
From my understanding, you have an array named fieldFilters that contains multiple objects with a field attribute (e.g. 'price'), a filterType attribute (e.g. '>') and a filterValue attribute (e.g. 50). If you are trying to do multiple range queries on different fields, then this will not work as stated in the documentation https://firebase.google.com/docs/firestore/query-data/queries#compound_queries. Solutions do exist for this problem, I suggest you read Frank van Puffelen response to the following question: Firestore multiple range query.
Solved it with this code:
let query;
category.queryParams.query.fieldFilters.forEach(function(fieldFilter) {
if (typeof query === "undefined") {
query = collection.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
} else {
query = query.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
}
});
I am working on my CouchDB project, where I want to create a specific view for my database.
It has proven that one of my key parameters has an ID as part of its name, but the other part is unique. ex "unique_ID_unique":"Value".
So brute force solutions like changing the name and/or how it is saved is not preferred.
To make it clear the ID is different for every entry (date).
I tried to modify it by using regex rules but it returns NULL for the key part.
emit(doc[/_unique$/], doc['something.else']);
Does someone have any idea why it is like that?
P.S: I already had a question like this yesterday, but due to the insufficient information that I gave, it led to wrong answers and I had to delete it.
Let's say you have a function that extract the unique key from a particular one:
var key = "blabla_120391029301923_blabla";
var keySplitted = key.split("_"); //Value: ["blabla","120391029301923","blabla"]
var uniqueKey = keySplitted[2]; //Value: blabla
From this, you can create a view what will map each documents and index them with your key.
function(doc){
var keySplitted = doc._id.split("_");
if(keySplitted.length == 3){
emit(keySplitted[2]);
}
}
The previous map ids from this:
evening_01012018_food
morning_02012018_musc
evening_01022018_food
To this:
food
musc
food
UPDATE
From offline discussion, I was able to understand that the objects to index had this content:
{
"_id": "doc_1",
"_rev": "1-89d017d9e5c82ee56d9beec2756fec99",
"type": "googrtt",
"ssrc_3685071425_send.bytesSent": "33621"
}
So with this document, the properties had to be split.
The final view content looks like this:
function (doc) {
for(key in doc){
var splitKey= key.split('_');
if(splitKey.length === 3){
emit(splitKey[2],doc[key]);
}
}
}
I have an object array:
user :[
{
name: String,
username: String
}
]
I want to view every change either to name or username.
I found underscore _.pluck only does the trick for one property (_.pluck(user, 'name')
Is there another way to have the list of both values?
With pluck you can only use one property, it simply isn't made to retrieve multiple. The method you would want to use is map, as suggested in this relevant question + answer: How to pluck multiple attributes from a Backbone collection?
Assuming you want the following output [['nameA','usernameA'],['nameB','usernameB'],...]], you could use map in the following manner:
var myResult = users.map(function(user) {
return [user.name, user.username];
});
NOTE: I changed the variable user to users to make more sense with your data.
I'd like to use the filters to return all values that have an empty string for a particular field.
That is, in the code:
var groups = $filter('unique')(addresses, 'country');
var groupedByCountry = [];
angular.forEach(groups, function (value, key) {
var selectedGroup = value['country'];
var grouped = $filter('filter')(addresses, { country: selectedGroup });
this.push(grouped);
}, groupedByCountry);
As can be seen in http://jsfiddle.net/b7cjM/, it creates groups as expected where a country is specified, but I'd like the last group to contain only the addresses that have no country specified (instead of, as currently, a group of all addresses in existence).
Is this possible using angularjs?
**
As suggested by punund, I wrote a 'groupBy' filter [a naive conversion of the 'unique' filter: http://jsfiddle.net/b7cjM/1 ], which is a much simpler solution to my problem than the one I was attempting. I suspect a custom 'filterByEmpty' of this type is also the solution for the problem as presented.
underscore or lodash may work better for you:
groupedBy = _.groupBy($scope.addresses, 'country');
Empty strings are properly taken into account, and you don't need the second filter. Another option would be to implement a kind of "groupBy" angular filter yourself.