Appending to multiple ids to api call - javascript

So I have a call I can make to an api that allows me to toggle whether or not a user has a vip role using this endpoint.
ApiService.patch('/admin/vip?user_id=1&user_id=2')
It can target multiple users at once by appending "user_id=:id" after the question mark with a "&" in between each users id. How could I make a variable of some kind that uses a loop to get the correct user ids and apply "user_id=" before their actual IDs and "&" between each user?
I can handle looping to get the IDs but not sure how to put "user_id=" before each id and "&" after each ID (Only if there is another user_id after the last one)

I'd say the easiest way to achieve this is by having a comma separated value for user_id. In your code you could then do something like query.user_id.split(',') and get an array of all the user IDs you passed in.

Use an array to store ids and then map the array to create the string, using a condition to skip the first. Or use URL Search Params

Related

elastic-builder: how to retrieve all unique value of one specific field

I am completely new using elastic search with current project(elastic-builder is used) and documentation of elastic-builder is bit confusing.
I need to retrieve all unique values of one specific field, say creator of tasks stored on server, how should I compose the query to achieve this?
You can use the terms aggregation to get all the unique values sorted by count from a field. If you use the default mapping for your index, your creator field should have a subfield creator.keyword that you can use for the terms aggregation.

mongoose query with different input

I want to check in a db if 'test' exists in a document, in a specific field. If exists, I want to increment it by 1, to look like this 'test-1', and then check again in the db, and repeat until it's not found, than save it there.
I don't have a problem with the increment part, just with the mongoose/mongodb part where I need to re-query with the new value. Is there any way that can be done in the same query, multiple times?
Edited: I want to create a new field based on the name, but I want this field to be unique, because I want to use it as an id.
So, for instance, if I have something like name: 'John Smith' I want to create a new field in my document like this: uniqueId: john-smith, for the same document. The problem is, if another John Smith is inserted in my collection, I want to check if the uniqueId john-smith is available, if not I want to append a -1 to it, so it will look like this john-smith-1, and so on, until a john-smith-(number) is not found, then I will know the id is unique and save it to the document.
One idea would be to use a Model.find() inside of a recursive function with the uniqueId, and repeat until a document is not found. But I was wondering if there might be another way, maybe something less complicated?
mongodb v4.2.3

underscore .find() multiples with same key value

I am currently trying to filter an object via POST from a form the form is made of checkboxes and as a result I need to be able to search an array of objects for multiple values on the same key.
My POST looks similar to this,
{
tax_year: ['2016/17', '2017/18'],
status : ['completed'],
user : [1,4,78]
}
How would I go about search an array of objects and returning all the objects that have a matching key and value? I know I can do a single find with underscore like so,
var result = _.where(historyData, {tax_year: "2016/17"});
but I have no clue as to how to search for multiple matching keys and values?
It seems that you're matching on both of the values in an array value (regardless of whether there are more array values or not). If that's true, you might have to use the _.filter() method, where you can provide whatever logic you need in the 'predicate'. Alternatively, you could add a unique identifier on the server side to each record and match on that.

Better way to refer to entries in firebase

As far as I know, firebase assigns automatically an unique ID to every new entry in the database. However - these ids are really long and not good looking.
Whats more - I have to refer to them somehow, so currently when Im doing a get request, e.g. to get one entry Im doing something like:
/getEntry/L4Cu7UOENIivnB2bgt
And it's fine, since user doesn't see it anyways.
Hovewer, when making routes to every entry in my app, again I have to refer to specific entry by it's id. So e.g. if Im on route of specified element, e.g.:
http://myapp.com/users/L4Cu7UOENIivnB2bgt - it doesn't look very well if not ugly. If I would make my db in e.g. SQL or NoSQL, I would be able to assign an id by myself so it would increase from 1 and so on.
Q: Am I able to change these long id's somehow? It has to be fixable somehow... Thanks.
Yes you can set your own unique key. Say you have unique usernames for each user then you can do
firebase.database().ref('users/' + userName).set({
firstName: name,
email: email,
profile_picture : imageUrl
});
or you can create your own unique ids and use instead. But there is no auto incremental ids.
Using set() overwrites data at the specified location, including any child nodes.

Use array or JSON to keep track of unique list

I have an object in Parse .com database that can receive thumbs up votes. I want to keep track of which users have provided a thumbs up to prevent the same user doing it again. I am thinking of adding a field to my object called "voters" that will hold the list of voting users.
Should this be an array of usernames or a JSON with keys as the usernames?
Why would I use an array if i could use a json map and do O(1) lookup of the names on the keys?
ie object.voters[uername] = undefined then no votes?
Use Array if you need just the name of the voter .
Use Json if you need more info than just the name .
Why is that if u need just the name just making an array with the voter names first will be easy second it will be faster t process .
but if you need something like
voter={"name":"foo","age":"25"}
as you see we can use the powerof json to save multiple info about one user and anyway each voter must be within a single array so you can iterate over them
UPDATE---
to find the index of a name in an array just use array.indexOf("the name") what this will return is the index number of the name if the name doesnt exist it will return just -1 so a simple if else can handle this
UPDATE---
And here i found a performance test to compare which is faster to lookup whitin an array or an object and seems arrays still win this.
And here is another source for reading

Categories

Resources