I have two api calls , with the only difference being in the array parameters :
// it is used for displaying whole list
Query 1 : limit=25&var1=[deleted=false]
//it is used for displaying on basis of id
Query 2 : limit=25&var1=[deleted=false,id=10005]
I tried using term , also something like but it takes both the query as one
var1: term({
generate: '[deleted=false]',
matcher: '[deleted=false]'}),
var1: term({
generate:'[deleted=false,id=10005]',
matcher:'[deleted=false,id=10005]'}),
Also , i passed the exact query but it doesn't work
Any suggestions on how to do it ?
If you want to using dynamic legnth array type by parameter in javascript, Use this.
var [deleted, id, ...any] = parameter;
console.log(deleted, id);
In case, if you throw parameter([deleted]), log be printed by "deleted, id is undefined".
Related
Using vanilla javascript, I'm using Mongo's dpd.js for querying the db.
In this snippet, I am trying find all matches for "design" in the column roleConsiderations within the collection techniques.
The values in roleConsiderations are in arrays - ["development", "design", "content"] and all of my different attempt to query for "design" fails.
dpd.js query results in empty response:
var query = {"roleConsiderations": "design"};
dpd.techniques.get(query, function (result) {
console.log(result);
});
Attempting to query through the url only returns exact matches (where "design" is the only value in the array).
http://my.mongo.db/techniques?roleConsiderations=["design"]
So how to query a mongodb column filtering for a value in an array?
You can use $in operator to specify the elements you want the returned objects to contain in specified field.
const query = { roleConsiderations: { $in: [ 'design' ] } };
Horrible syntax, I know, but it should work.
For more reading, please refer to this.
So I'm new with Axios and I'm trying to make a Discord bot that takes info from an api website and uses it. The issue is the data given looks like:
{"status":1,"size":1,"result":[{"number":"9999", and so on.
How do I get the data inside of the [ ]? I tried:
var teamNumber = response.data.result.number
but it doesn't work.
I can get response.data.status, but not result.number
TLDR: how to get 9999 from {"result":[{"number":"9999", ?
result is an array; if you want to get the first element from it (and get that element's "number" field) you'd do:
console.log(response.data.result[0].number);
If you want to loop over them you'd do:
response.data.result.map(el => {
console.log(el.number);
});
From pg-promise's example, one can format a query like below, where ${this~} becomes all of the keys in the object that is the second parameter of "format()".
// automatically list object properties as sql names:
format('INSERT INTO table(${this~}) VALUES(${one}, ${two})', {
one: 1,
two: 2
});
//=> INSERT INTO table("one","two") VALUES(1, 2)
Is it possible to also get all of the values of the object, without explicitly typing all of them? I want to do it like below (should do the same thing as the snippet above, but without typing all of the values):
format('INSERT INTO table(${this~}) VALUES(${this#})', {
one: 1,
two: 2
});
Is it possible to also get all of the values of the object, without explicitly typing all of them?
No, it is not possible, because while column names require the same-type SQL Name escaping, values do not, they require templating that's possible only via explicitly defined variables.
I want to do it like below...
For that you should use the helpers methods of the library:
const cs = new pgp.helpers.ColumnSet(['one', 'two'], {table: 'my-table'});
const query = pgp.helpers.insert(values, cs);
I am using Firebase to create a search function as follows
Firebase.database.ref('database-location').once('value').then(function(snapshot){
//do some filtering on results
});
However i want to dynamically sort the results using either:
.orderByKey()
or
.orderByChild('price')
I tried using bracket notation to select the above (the vars are decided from an if statment):
var orderByType = 'orderByChild'||'orderByKey'
var orderByVal = 'price'||undefined
Firebase.database.ref('database-location')[orderByType](orderByVal).once('value').then(function(snapshot){
//do some filtering on results
});
This works well for sort by value, but when using sort by key, firebase returns an error : "Query.orderByKey failed: Was called with 1 argument. Expects none."
Is there a way to dynamically remove the argument completely?
or any other way to switch between the two order by types?
Update a nested collection in meteor is not a problem (and it is described here : Updating a nested property on a collection object with $set)
Basic way to do it :
Collection.update({sel}, {"$set" : {"address.city": "new address"}});
But what if I want to describe my path with variables ?
This one obviously does not work :
var cityName = "NYC";
Collection.update({sel}, {"$set" : {"address." + cityName: "new address"}});
Sadly this one does not work either:
var path = "address.NYC";
Collection.update({sel}, {"$set" : {path: "new address"}});
Neither does that one:
var object = {"address.NYC": "new address"};
Collection.update({sel}, {"$set" : object});
Well, actually, it works, but not the way I want it. It replace entirely the "address" object, removing the other properties.
Any ideas ?
Is there a way to select the field I want to update in the query part ?
It doesn't work because you can't use dynamic keys for object literals in javascript. You need to use bracket notation and build up the object you want to use in the update. For example:
var city = 'NYC';
var object = {};
object["address." + city] = 'new address';
MyCollectionName.update(idOfObjectToUpdate, {$set: object});
Your last example should work, assuming Collection is actually the name of a collection, and {sel} is the correct selector for what you are trying to do.