Creating Functions in MongoDB Console [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
This query returns a result db.graduates.find({student_id: '2010-01016'}).pretty()
and then I built a function
function findStud(name,value){ return db.graduates.find({name:value}); }
on the mongo shell when I run this findStud("student_id","2010-01016")
it does not display the results

You need to compose an query object with the key being the value in the nameparameter and value being the value in the parameter value.
function findStud(name,value){
var query = {};
query[name] = value;
return db.graduates.find(query);
}
By default when you don't do this, name is considered to be a String literal and the query gets executed as db.graduates.find({"name":value}); which searches for a key named name with the specified value, causing the query to fail.
See Also: Mongodb doesn't not update when I use like this

Related

How to to make a varargs statement in javascript [duplicate]

This question already has answers here:
JavaScript variable number of arguments to function
(12 answers)
Closed 1 year ago.
In javascript I am in situation where i need to make variable arguments based on a length of an array, below is an sample code
function getValesList(json){
return getValues(json[0])+getValues(json[1])+getValues(json[2]);
}
function getValues(json1){
let valueList = Object.values(json1);
let valueListPipe = valueList.join("|");
return valueListPipe+lineSeparator;
}
where json is an array of JSON objects and I need to make a pipe delimiter file based on the length of incoming array. How to make it dynamic where I can do like a varargs in JAVA
If you're just passing N arguments of the same type, you can use the rest feature of Javascript for function arguments.
function getValuesList(...json){
return json.map(j => getValues(j)).join("");
}
This allows you go pass any number of separate arguments as in getValuesList(o1, o2, o3, o4) and the json parameter within your function will automatically be an array of however many arguments were passed.

How to access a particular path/adress inside an object? [duplicate]

This question already has answers here:
access object through dot-syntax string path
(2 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 2 years ago.
I am having a problem which I think I might have figured out before how to do it but I can't remember now and can't figure it out.
Let's say we have an object thats a few levels deep, meaning it has as values other objects which also have as some of the values objects and so on.
Now how could I make a function to which I pass the object and and adress inside it and I can access the value at that location inside the function like this:
const getValueAtAdress = (object, 'country.city.rules') => {
return //here I need to return the value at object.country.city.rules.
}
Am I missing something obvious?
I thought I'd mention here for posterity that what helped me was the answer using the reduce which is exactly what I used before but I could not remember:
Example that I am using for my particular problem:
let stateLocation = address.split('.').reduce((acc, cur) => acc[cur], state);
Your code shows a function declaration but you can't declare an argument name in quotes
You can however call a function and pass a string.
In that case, you just need to split the string into an array and then loop over that array, building up a "chained" set of string indexes that can be passed to the object. The String.split() and Array.reduce() methods are the key.
let obj = {
county: {
city: {
rules: "Strict"
}
}
};
const getValueAtAddress = (object, countyCityRules) => {
// Split the string at the dots to form an array...
// The loop over that array and reduce it with an
// accumulator that is then applied to the object.
return countyCityRules.split(".").reduce((acc, cur) => acc[cur], obj);;
}
console.log(getValueAtAddress(obj, "county"));
console.log(getValueAtAddress(obj, "county.city"));
console.log(getValueAtAddress(obj, "county.city.rules"));

Firestore Cloud functions use variable in property for update document [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 2 years ago.
Trying to use a variable as property when updating a document via Cloud functions.
Maybe I miss the syntax here or is it simply not possible.
var value = 'myValue'
var name = 'myVariable'
admin.firestore().collection('mycollection').doc('mydocument').update({ name : value});
The document is updated but it shows then name = myValue and would like to see in the document myVariable = myValue.
What would be the syntax to achieve that to be flexible?
Got it.
var value = 'myValue'
var name = 'myVariable'
admin.firestore().collection('mycollection').doc('mydocument').update({ [name] : value})

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

How can I turn a string into a key? [duplicate]

This question already has answers here:
Javascript create variable from its name
(5 answers)
Closed 8 years ago.
I need to run this line of Javascript:
#model.save({ name: #some_element.val() })
But the key, which in this case is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.
var obj = {};
obj[varName] = #some_element.val();
#model.save(obj);

Categories