How to access value of variable in Mongo? - javascript

var a = "act";
db.s.findOne( {
BrandId: doc.BrandId,
a : {$exists:false}
} ).a;
I try to pass the value of variable "a" to my MongoScript. But it looks it can not be simply used. anyone can help?

Build your query step by step and do not use . use [] instead
var a = 'act';
var query = {BrandId: doc.BrandId};
query[a] = {$exists:false};
db.s.findOne(query)[a];
Note:
query.act == query['act'].
Just some javascript tricks.

Related

How to store value in localStorage with certain format

how can I have a localStorage value stored as
I have the value as:
ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew
but its like it has an arrow to expand and shows the same value only without the {}, an also how do I insert those keys with the slash and 2dots, have the value inside "", I thought it was an object but after trying to insert the value in a variable
var lsvalue = {
/: "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew",
}
localStorage.setItem('rmStore', JSON.stringify(lsvalue))
but it did not work, any thoughts?
tried to code mentioned
Store it as a string, the debug tool is automatically interpreting this as an object.
var lsvalue = '{"/":"ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew"}';
localStorage.setItem( 'rmStore', lsvalue )
If I'm understanding this correctly, you want your value to be just this string: "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew".
So, you can just assign that string alone to lsvalue, and set it to localStorage:
var lsvalue = "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew";
localStorage.setItem('rmStore', lsvalue);
Or just set that string as the value:
localStorage.setItem('rmStore', 'ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew');
Both of the above solutions result in the below:
But, if you're looking to set an object to localStorage:
var lsvalue =
{
'storedObject' : "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew"
}
localStorage.setItem('rmStore', JSON.stringify(lsvalue));
This setup results in the below:

Stringify a key value pair in Javascript and session storage

I have code as follows:
var employee = {};
employee["fullname"] = document.userForm.fullname.value;
employee["license"] = document.userForm.license.value;
employee["address"] = document.userForm.address.value;
employee["dob"] = document.userForm.dob.value;
employee["city"] = document.userForm.city.value;
employee["state"] = document.userForm.state.value;
employee["zip"] = document.userForm.zip.value;
employee["sex"] = document.userForm.sex.value;
sessionStorage.setItem("session", JSON.stringify({"employees": [employee]}));
Is it correct to store it this way? Also if this is correct , how do I retrieve the information using getItem?
retrieve it with :
var mySessionData = sessionStorage.getItem("session");
var mySessionDataParsed = JSON.parse( mySessionData );
edit
Is it correct to store it this way
//sessionStorage.setItem("session", JSON.stringify({"employees": [employee]}));
sessionStorage.setItem("session", JSON.stringify( employee ) );
// this way is suffisant !
// but if you prefer an array you can go with your way too !
You can also use dot and/or bracket notation.
sessionStorage["session"] = JSON.stringify({"employees": [employee]});
// or
sessionStorage.session = JSON.stringify({"employees": [employee]});
and the same goes for the right hand side of an assignment.
data = sessionStorage["session"];
// or
data = sessionStorage.session;
No need to invoke the get and set item methods as you just add needless overhead.

JS : Objects into array

i'm trying to create object named client. and put it into a array. And after this, read the name of the 1st client.
Exemple :
client1 {nom : "marco", prenom : "chedel", adresseMail : "ssss#ggg.com"};
and put this client into an array like bellow.
listClients[client1]
what i did :
var listClients = [];
var client1 = {nom : "chedel",prenom:"Marco",adresseMail:"marco#gmail.com"};
listClients.push(client1);
var client2 = {nom : "De Almeida",prenom:"Jorge",adresseMail:"jorge#gmail.com"};
listClients.push(client2);
function afficheClients(tableau){
for (i=0;i<tableau.length;i++)
{
var c = tableau[i];
document.write(c[adresseMail]);
// This ^^ doesn't work, it says :adresseMail isn't definied in c
}
}
afficheClients(listClients);
You're treating adressMail as a variable and not as a string:
use
document.write(c["adresseMail"]);
There are two ways to access properties of an object:
obj.prop
obj['prop']
You are doing the following mixture which doesn't work: obj[prop].
Fix your code to c.adresseMail or c['adresseMail'] and it will work.
Either reference it using a string:
document.write(c['adresseMail']);
or using dot notation:
document.write(c.adresseMail);
And, yes - document.write should be avoided.

using a variable in mongodb update

Using Meteor, I'm trying to perform an update like the following:
Items.update(Session.get('selectedItem'), {'$set': {'directions.0.name': area.value}})
But I'm struggling with how to set the array index of directions dynamically, with something like this:
var index = //a value determined dynamically
Items.update(Session.get('selectedItem'), {'$set': {'directions[index]name': area.value}})
This doesn't work because [index] is wrapped in a string. I also tried to form a custom string, like this:
var string = 'directions.'+itemIndex+'.name'
Items.update(Session.get('selectedItem'), {'$set': {string: area.value}})
But that doesn't work. Any idea on how to do this?
You need to build up your $set object programmatically:
var setModifier = { $set: {} };
setModifier.$set['directions.' + index + '.name'] = area.value;
Items.update(Session.get('selectedItem'), setModifier);
Update
If your JavaScript environment supports computed property names (e.g. node.js 4+), you can do this in one step:
Items.update(Session.get('selectedItem'), { $set: {
['directions.' + index + '.name']: area.value
}});

Is it possible to append value in an existing object - jquery

Consider i am having an object "req".
When i use console.log(req) i get
Object { term="s"}
My requirement is to append an value with the existing object.
My expected requirement is to be like this:
Object { term="s", st_id = "512"}
Is it possible to do the above?
If yes, how ?
Thanks in advance..
There are several ways to do it;
Plain javascript:
var req = { "term" : "s" };
req.st_id = "512";
Because javascript objects behaves like associative arrays* you can also use this:
var req = { "term" : "s" };
req["st_id"] = "512";
jQuery way $.extend():
var req = { "term" : "s" };
$.extend(req, { "st_id" : "512" });
You can do this in a couple of ways:
req.st_id = "512";
or
req["st_id"] = "512";
The second of these is especially useful if the variable name is dynamic, e.g. a variable could be used instead of the string literal:
var key = "st_id";
req[key] = "512";
Yes this is possible, to add properties to a value simply use the following syntax:
req.st_id = "512";

Categories