Mongodb '$where' query using javascript regex - mongodb

I am trying to reproduce the REPLACE function in sql on mongodb.
My collection 'log' has this data in the 'text' field.
[01]ABC0007[0d0a]BB BABLOXC[0d0a]067989 PLPXBNS[0d0a02]BBR OIC002 L5U0/P AMD KAP 041800 T1200AND 2+00[0d0a0b03]
All I'm trying to do is remove the '[..]' using regex (javascript) and use contains('PLPXBNSBBR') like this so that the expression return true per the javadocs in mongo documentation.
This query below successfully works and returns the matching rows.
db.log.find({"$where":"return this.message.replace(new RegExp('0d0a02'),'').contains('PLPXBNS[]BBR') "});
However, I would need to remove the '[..]' and match like this PLPXBNSBBR.
These are the ones I tried unsuccessfully
db.log.find({"$where" : " return this.message.replace( new
RegExp('\[.*?\]', 'g'), '' ).contains('PLPXBNSBBR') " });
db.log.find({"$where" : " return this.message.replace( new
RegExp('/[(.*?)]', 'g'), '' ).contains('PLPXBNSBBR') " });
db.log.find({"$where" : " return this.message.replace( new
RegExp('//[.*//]'), '' ).contains('PLPXBNSBBR') " });
db.log.find({"$where" : " return this.message.replace( new
RegExp('[.*?]'), '' ).contains('PLPXBNSBBR') " });
From the earlier discussion it appears that if I can pass the pattern as /[[^]]+]/g, it should strip the [..] but it is not doing that and not returning the matching rows.

Okay, I was able to use chaining replace successfully to get my desired results.

Related

HTL AEM conditional OR operator issue

I want to be able to use the "||" OR operator in this context
inside a schema, i have two dates I need to pull from the JCR content if the value in the field has not been the author
<script type="application/ld+json" >
"datePublished": "${properties.datePublishedArticle #context="html" ||'yyyy-MM-dd' # format=currentPage.lastModified }",
"dateModified": "${properties.dateModifiedArticle # context="html" || 'yyyy-MM-dd' # format=currentPage.lastModified}"
</script>
org.apache.sling.api.scripting.ScriptEvaluationException:
mismatched input '#' expecting {'}', '.', 'in', '&&', '||', ',', '['} in line 67 where datepublished is located.
In order words, if author has not authored a value, it will take the value from the jcr content. They work fine when done separately.
Do not understand the error that is indicating.
Have you tried to use it like this:
<script type="application/ld+json" >
"datePublished": "${properties.datePublishedArticle ||'yyyy-MM-dd' # context="html", format = currentPage.lastModified }",
"dateModified": "${properties.dateModifiedArticle || 'yyyy-MM-dd' # context="html", format = currentPage.lastModified}"
</script>
I would think that the error is happening because you shouldn't be repeating the '#' block in the same HTL statement.
Although #atgar's solution does not throw an error, I think it does not do what the author asked for: output either the authored date or the JCR (lastModified) date.
Something like this would be closer to what he needs:
<script type="application/ld+json" >
"datePublished": "${'yyyy-MM-dd' # context='html', format = (properties.datePublishedArticle ? properties.datePublishedArticle : currentPage.lastModified) }",
"dateModified": "${'yyyy-MM-dd' # context='html', format = (properties.dateModifiedArticle ? properties.dateModifiedArticle : currentPage.lastModified)}"
</script>
Please note, when using HTL date formatting, the actual values should be in the format option.

MYSQLi PHP connect

Is the following a valid mysqli php connect insert statement?
if($mysql_query = $conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')"))
{
?>
<script>alert('successfully registered ');</script>
<?php
}
else
{
?>
<script>alert('error while registering you...');</script>
<?php
}
}
However I am getting error as "error while registering you..."
Thanks
Issue is resolved. There was an issue with the "user_id" column primary key which was being updated duplicate(0 value) with this error "Duplicate entry '0' for key 'PRIMARY'. Upon updating user_id column with auto increment in phpmyadmin, the INSERT query got updated successfully.
Using the below code to check for errors helped.
die(mysqli_error($conn));
A simple example:
<?php
//Create the connection
$con = mysqli_connect("HostName","UserName","password","DBName") or die("Some error occurred during connection " . mysqli_error($con));
// Write query
$strSQL = "SELECT username FROM MyTable";
// Execute the query.
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["username"]."
";
}
// Close the connection
mysqli_close($con);
?>
Can you paste the whole block with connection statement?
Otherwise SQL statement looks fine. Excepct you should escape inputs to MySQL
if($mysql_query = $conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')"))
Why using if($mysql_query = $conn->query ?
better
if($conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')") === TRUE)

Postgres, meteor and fibers? Argggh not friends?

I'm trying to use postgres with Meteor using Coffeescript. Unfortunately since two days with no success. There is so much confusion in my head right now..
I'm having the error Error: Meteor code must always run within a Fiber. exactly where I do the Meteor.WrapAsync inside runQuery().
I'm using the npm module 'pg' with npmRequire.
So my service code is like this:
dropTables: =>
identifier= 'Drop tables'
Meteor.wrapAsync(runQuery) "DROP TABLE IF EXISTS " + mapping + " ;" , identifier
console.log 'Finished running query'
runQuery = (theQuery, identifier) =>
pg = Meteor.npmRequire 'pg'
console.log 'About to connect to postgres for ', identifier
pg.connect conString, (err, client, done) ->
_logServices.Info 'About to query for ', identifier
myResult = Meteor.wrapAsync((callback) ->
client.query theQuery , (err, result) ->
if err
_logServices.Error 'Error while '+ identifier +' with the query: ', theQuery
else
_logServices.Info "Success " + identifier
_logServices.Info 'closing connection for ', identifier
callback(null, 1)
)()
done()
pg.end()
console.log "still here!"
What I'm exactly trying to achive is to run the postgres things asyncronusly. In order to do one thing per time, but probably I'm missusing the fibers.

You have an error in your SQL syntax with tinymce

Hi my script ading system is working perfectly but when i use tinymce plugin so i get this error please help fast if anyone can solve that
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'color: #008000;">dsfdsfsdfsfdfsdfsdfsd", zone1 = "http://www.k' at line 8
this is my adding note code in userpanel
function add_note($note_title, $note_detail, $orient, $c_s, $zone1, $zone2, $zone3, $zone4, $z4_1, $z4_2, $z4_3, $zone5, $z5_1, $z5_2, $z5_3, $zone6, $z6_1, $z6_2, $z6_3, $s_m, $sm_back, $sm_text, $sm_direction, $sm_speed) {
global $db;
$query = 'INSERT into notes VALUES(NULL, "'.date("Y-m-d").'", "'.$note_title.'", "'.$note_detail.'", "'.$orient.'", "'.$c_s.'", "'.$zone1.'", "'.$zone2.'", "'.$zone3.'", "'.$zone4.'", "'.$z4_1.'", "'.$z4_2.'", "'.$z4_3.'", "'.$zone5.'", "'.$z5_1.'", "'.$z5_2.'", "'.$z5_3.'", "'.$zone6.'", "'.$z6_1.'", "'.$z6_2.'", "'.$z6_3.'", "'.$s_m.'", "'.$sm_back.'", "'.$sm_text.'", "'.$sm_direction.'", "'.$sm_speed.'", "'.$_SESSION['user_id'].'")';
$result = $db->query($query) or die($db->error);
return 'Note added successfuly!';
}//add notes ends here.
Here is this link of Full Code of my notes.php
http://pastebin.com/a6sNeXD0
all users Attention here for soloution if they want to use tinymce and they get error like this so they must have to enter there table like this
mysql_real_escape_string($sql_table).
so my code is like this
$query = 'INSERT into notes VALUES(NULL, "'.date("Y-m-d").'", "'.$note_title.'")
so i just add mysql_realescape_string
$query = 'INSERT into notes VALUES(NULL, "'.date("Y-m-d").'", mysql_real_escape_string($notetitle))
and it works but it show me
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established i
so for that you have to add this in before that $query thing :)
$con = mysql_pconnect("localhost","users","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
and you'r done ..... thanks for all help

How to remove collection named 'group'?

I accidentally created a collection named 'group'.
How do I remove it.
When I give the following in the mongo console
db.group.drop()
I get the following error
Fri Jun 7 16:36:39.630 JavaScript execution failed: TypeError: Object function ( parmsObj ){
var ret = this.runCommand( { "group" : this._groupFixParms( parmsObj ) } );
if ( ! ret.ok ){
throw "group command failed: " + tojson( ret );
}
return ret.retval;
} has no method 'drop'
The problem is that group is a method on a database object. So, db.group cannot be used to get the actual collection named group. Instead, use .getCollection():
db.getCollection('group').drop()