In MongoDb Shell
db.keyword.update({"state":"UT"}, {$unset:{'abc.def':1}});
unsets removes def from abc . However, if I do it like this :
var key = 'def'
var key1 = 'abc.'+key
db.keyword.update({"state":"UT"}, {$unset:{key1:1}});
doesnt unset def.
How do I get to unset "abc.def" by passing key1 ?
Yes, this is how json parser works. By the standard, hash keys must be enclosed in quotes, but some parsers are too forgiving and allow you to omit them. So, this is how mongo sees your code.
db.keyword.update({"state": "UT"}, {"$unset": {"key1": 1}});
You can get around this problem by constructing the hash manually. Something like this:
var key = 'def';
var key1 = 'abc.' + key;
var mod = {"$unset": {}};
mod["$unset"][key1] = 1;
db.keyword.update({"state": "UT"}, mod);
Related
https://repl.it/repls/DeadlyRemarkableKeys
const test1 = {["123"]:{stuff: '123'}};
console.log(test1);
var test2 = [];
test2["123"] = {stuff: '123'};
console.log(test2);
I would like to get proper array with objects linked to keys, like in test2 variable, however written in style of test1 ? test1 results with test1[0][123], whereas I need test1[123].
Thank you
Using an array as key of an object looks weird.
Probably this is what you're looking for:
const test1 = {'123': {stuff: '123'}};
console.log(test1['123']); // prints {stuff: '123'};
I am using pgp.helpers.insert to save data into PostgreSQL which is working well. However, I need to return values to present in a response. I am using:
this.collection.one(this.collection.$config.pgp.helpers.insert(values, null, 'branch'))
which returns no data.
What I want to be able to do is return the branch id after a successful insert, such as:
INSERT into branch (columns) VALUES (values) RETURNING pk_branchID
Simply append the RETURNING... clause to the generated query:
var h = this.collection.$config.pgp.helpers;
var query = h.insert(values, null, 'branch') + 'RETURNING pk_branchID';
return this.collection.one(query);
You must have a large object there if you want to automatically generate the insert. Namespace helpers is mostly valued when generating multi-row inserts/updates, in which case a ColumnSet is used as a static variable:
var h = this.collection.$config.pgp.helpers;
var cs = new h.ColumnSet(['col_a', 'col_b'], {table: 'branch'});
var data = [{col_a: 1, col_b: 2}, ...];
var query = h.insert(data, cs) + 'RETURNING pk_branchID';
return this.collection.many(query);
Note that in this case we do .many, as 1 or more rows/results are expected back. This can even be transformed into just an array of id-s:
return this.collection.map(query, [], a => a.pk_branchID);
see: Database.map
How to make case sensitive query with nodejs + pg
I want to select column content == 'a#gmail.com',
but it seems become select column == 'a#gmail.com'?
[error: column "a#gmail.com" does not exist]
code
var userEmail = 'a#gmail.com';
var query = 'SELECT EXISTS(SELECT * FROM "User" WHERE "Email" = "'+userEmail+'")';
dbClient.query(query, function(error, result) {
...
For use binding parameters you must number it begining with $1 (then $2 and so), then put the parameters in an Array:
var query = 'SELECT EXISTS(SELECT * FROM "User" WHERE "Email" = $1)';
dbClient.query(query, [userEmail], function(error, result) {
Always pass the parameters in an array. Is most secure.
Remember do not pass a function to query if you have a very big table unless you want to read all the table before returns the control to de function. Else you can use the "on" event or use a promise way (like https://www.npmjs.com/package/pg-promise-strict)
This doesn't have anything to do with case. The problem is that you're putting the email address in double-quotes, and in (most varieties of) SQL double-quotes indicate a column name or table name. That's why the error message says column "a#gmail.com" does not exist.
Use single-quotes around values:
var userEmail = 'a#gmail.com';
var query = 'SELECT EXISTS(SELECT * FROM "User" WHERE "Email" = \'' + userEmail + '\')';
Ideally, though, you should just use parameter binding so you don't have to worry about quoting values at all. When you use string concatenation to build SQL queries you very often open yourself up to SQL injection attacks.
This is the keyMirror package found here
https://www.npmjs.com/package/keymirror
it defines it as A simple utility for creating an object with values equal to its keys
Input: {key1: val1, key2: val2}
Output: {key1: key1, key2: key2}
But why would i need to do this?
how is this different from say,
{OPTION_ONE:1, OPTION_TWO:2, OPTION_THREE:3}
Why would an application find the below transformation to the above input useful?
{OPTION_ONE:OPTION_ONE, OPTION_TWO:OPTION_TWO, OPTION_THREE:OPTION_THREE}
It's making an Enum object (https://en.wikipedia.org/wiki/Enumerated_type)
You could do, like you said {OPTION_ONE:1, OPTION_TWO:2, OPTION_THREE:3}, which is a similar Enum object, but what if you wanted to know if the value 6 was a value from the enum? You would have to loop over all the keys and check all the values.
But, if the keys and values match, then you can just check if the key exists in the object to know if the value also does.
var mirror = keyMirror({a: null, b:null});
var someValue = 'd';
var validValue = mirror[someValue] !== undefined;
I have a string var dictAsString:String = '["foo" : 123, "bar" : 456]' that I want to convert to a Dictionary (or NSDictionary, I'm not particular.) I've tried
var dictAsObj:AnyObject = dictAsString as AnyObject
var dictAsDict:NSDictionary = dictAsObj as NSDictionary
but that doesn't work. I've also tried
var dictAsDict:NSDictionary = NSDictionary(objectsAndKeys: dictAsString)
and
var dictAsObj:AnyObject = dictAsString as AnyObject
var dictAsDict:NSDictionary = NSDictionary(objectsAndKeys: dictAsObj)
Nothing seems to work, and I can't seem to find any help in the documentation. Any ideas?
That string resembles a JSON object.
You could replace the square brackets with curly brackets and use NSJSONSerialization class to get a dictionary out of it.
Worst case scenario, you should write a little parser.
I suggest using Ragel.
Both tasks are an overkill for a string like that, though.