How to Update A boolean value in mongo using mongoite in R - r

I am unable to update a bool value in mongo using mongolite. I have a bool value (FALSE) in r and I am trying to update a field in mongo which is currently having value true(mongo bool). But After making update command using mongolite, stored bool value true in mongo changed to string value FALSE(R bool type).

mongolite doesn't automatically update r booleans as mongo booleans.
this throws an Error: Invalid JSON object for me:
m$update('{"name":"foo", "$set":{"boolean":FALSE}}')
this inserts FALSE as string:
m$update('{"name":"foo", "$set":{"boolean":"FALSE"}}')
this inserts false as boolean:
m$update('{"name":"foo", "$set":{"boolean":false}}')
if you're making the update query programmatically you can do something like this:
```
my_boolean <- FALSE
my_updateQuery <- paste0('{"$set":{"boolean":',tolower(paste0(my_boolean)),'}}')
m$update('{"name":"foo", my_updateQuery)
```

Related

SQLite3 with Flask Insert only allowing 1 character

I'm using Flask and trying to insert names (text) into an SQLite3 DB. The DB has one table (guests) and one column (name). However, it will only accept 1 character during the insert; anything more is failing.
If I hardcode 'nm' as a value, it still fails, so I don't think it's the Html template. I can manually add a value of varying length to the DB with the 'DB Browser' app, so I'm really at a loss here. Here's a code snippet.
#app.route('/addrec', methods=['POST', 'GET'])
def addrec():
msg = "msg"
if request.method == 'POST':
try:
nm = request.form['nm']
with sqlite3.connect("database.db") as con:
cur = con.cursor()
cur.execute('''Insert into guests values (?)''', nm)
con.commit()
msg = "Record successfully added"
Second argument to cur.execute should be a tuple:
cur.execute('''Insert into guests values (?)''', (nm,))
Why? If, for example, you call that argument arg then arg[0] is the string value you require (nm): the first item in that tuple.
Otherwise (when passing nm instead of the tuple) it will read nm[0] which is the first character of the string nm.

parseLine(String nextLine, boolean multi) in opencsv

I am learning opencsv now,the method parseLine(String nextLine, boolean multi) in CSVParser is a little complex. What is the mearning of the field 'inField' in the class CSVParser? What does 'inQuotes' and 'fromQuotedField' denote in method parseLine(String nextLine, boolean multi); Thanks!
about line 112 in CSVParser : private boolean inField = false ;
about 348 line in parseLine(String nextLine, boolean multi):
boolean inQuotes = false;
boolean fromQuotedField = false;
The inQuotes tells the parser that the data it is reading is inside a quoted string - so if a delemeter character is met then it is part of the string instead of a signal to start a new field.
inField has basically the same functionality but it is a global variable (and it is not dependent on quotations). The reason openCSV needs to know this is that the CSVReader feeds the one line at a time - so if there is a line feed inside one of the fields then it will take multiple calls to the CSVParser to build one complete record. Hence the parseLineMulti.
The fromQuoteFields is a new addition for feature request #60. This is used with the null field indicator to detect if you have a set of empty quotes should the field be an empty string or null.

Asp.Net SqlDataReader value magically turns into null on attempt to access and use it

I have an sql command that depends on the results of other sql commands. There are five sql commands in this chain, but the problem occurs in the 4th.
The 5th command must save data to the archive table. When it has to run and take the completion_date value from the 4th command, it throws a null reference exception. Actually it says, that Reader4[0] can't be read because it has null value. That's wrong because it has value, because in the database these queries work fine and also because the condition if (Reader4.HasRows == true) is true, it means that WHERE statement is true in the 4th command, which also includes checking the completion_date! What's wrong with this asp.net?
Command4 = new SqlCommand("SELECT trade_date FROM Schedule WHERE traider_id=#traiderID AND good_id=#goodID AND position_id=#positionID AND status_id=1 AND completion_date IS NOT NULL", Connection4); //note the completion_date check
Command4.Parameters.Add("#traiderID", Convert.ToInt32(Reader3[0]));
Command4.Parameters.Add("#positionID", CurrentPosition);
Command4.Parameters.Add("#goodID", Convert.ToInt32(Reader1[0]));
Reader4 = Command4.ExecuteReader();
if (Reader4.HasRows == true) //this check is done successfully
{
Command5 = new SqlCommand("INSERT INTO Archive (traider_id, good_id, completion_date) VALUES (#traiderID, #goodID, #completionDate)", Connection5);
Command5.Parameters.Add("#traiderID", Convert.ToInt32(Reader3[0]));
Command5.Parameters.Add("#goodID", Convert.ToInt32(Reader1[0]));
Command5.Parameters.Add("#completionDate", Convert.ToDateTime(Reader4[0])); //Here is the problem
Command5.ExecuteNonQuery();
}
Reader4.Close();
Where are you startin to read from the reader?
i.e. where do you call
Reader4.Read();
after
Reader4 = Command4.ExecuteReader();

Querying RMongo with ObjectId

Is there a way to query in RMongo with an ObjectId?
Something like:
results <- dbGetQuery(mongo, "users", "{'_id': 'ObjectId('5158ce108b481836aee879f8')'}")
Perhaps by importing a bson library?
RMongo's dbGetQuery() function is expecting MongoDB Extended JSON syntax for the provided query string.
The MongoDB Extended JSON equivalent of ObjectId("<id>") is { "$oid": "<id>" }:
results <- dbGetQuery(mongo, "users", "{'_id': { '$oid': '5158ce108b481836aee879f8' }}")
Try the new mongolite package:
library(mongolite)
m <- mongo("users")
m$find('{"_id":{"$oid":"5158ce108b481836aee879f8"}}')
mongo.oid.from.string {rmongodb}
Create a mongo.oid object ftom a string
Package: rmongodb
Version: 1.5.3
Description
Create from a 24-character hex string a mongo.oid object representing a MongoDB Object ID.
Usage
mongo.oid.from.string(hexstr)
Arguments
hexstr
(string) 24 hex characters representing the OID.
Note that although an error is thrown if the length is not 24, no error is thrown if the characters are not hex digits; you'll get zero bits for the invalid digits.
Details
See http://www.mongodb.org/display/DOCS/Object+IDs
Values

How to test QueryString

I have a query string called propID and I wanna check if the passed value in it is a legal integer or not to avoid throwing an error that might reveal info about my database, how can I do it?
In other words, I want something like -but in vb.net- :
IF QueryString("propID").Content.Type = "int32" Then Proceed
You could use TryParse:
Dim myInt As Integer
If Int32.TryParse(QueryString("propID").Content, myInt) Then Proceed
Dim result as boolean
result = integer.tryparse(QueryString("propID"), myintegervariable)
boolean will return true if it parsed correctly (putting the value into your myintegervariable) and will return false if the parsing failed.
You can also write is as
if integer.tryparse(QueryString("propID"), myintegervariable) then
//continue going along with myintegervariable
else
//parsing didn't work
end if
You can just use Int32.TryParse.
You could try the 'is' keyword to check the type of on object.
If QueryString("propID").Content.Type Is Int32 Then Proceed
Otherwise Int32.TryParse would work as well.
C# version:
int _PropID;
if (int.TryParse(QueryString["propID"], out _PropID))
{
//Proceed with _PropID
}

Resources