I have a python script that receive (from a node.js script) an _id for a mongodb document as an argument. Using that value I'm trying to query the db and retrieve a document.
However, when i try to run the script it throws an error saying
"'xxxxxxxxxx' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string".
my script where the error is causing:
result = db.req.find_one({"_id": ObjectId(sys.argv[1])})
When i check the type() of the sys.argv[1] it says str. I thought wrapping the string around ObjectId should do the trick.
value of sys.argv[1] when printed: '"5902fbdd4d2f430dfe2dded4"'
Anyone one know whats the reason causing the issue?
Thanks in advance.
Resolved:
I removed the JSON.stringify() bit from the passing value in the node.js script. All seems fine now.
MongoDB allows storing "_id" in "ObjectId" format.
Related
I am really confused and hope someone can help.
I am working on a program that, when retrieving records from a SQL Server database, some of the fields can come back as null. One of the fields is named LaserName. Looking at the locals Window, the LaserName variable appears to just be a empty string.
But, when I use this code:
LaserName != ''
The code simply bypasses it as if the variable was not empty. When I looked at the type of that variable:
print(type(LaserName))
...the type returns <type 'DBNull'>. How do I look for DBNull in Python? Or is there a more elegant way to look for NULL values returned from a database?
Converting the type to a string seems to have alleviated my problem.
if "DBNull" in str(type(wgtper))
Currently I am working to convert my py-2 project in py-3 & during this conversion I have faced below kind of error.
Partner.objects.filter(name__iexact = name_kv).count()
When I am running above query in py2 it working perfectly & getting output '0', means getting empty list.
When I am running above query in py3 it showing below kind of error.
django.db.utils.ProgrammingError: function upper(bytea) does not exist
LINE 1: ...alse AND UPPER("partners_partner"."name"::text) = UPPER('\x4...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I have searched lot online & SO other questions but but not able to find any solution.
I figured out that it must have been python version problem & I am getting above error when my ORM query does not have any records.
Try to convert your variable name_kv to string type using str(name_kv).
Update your query like Partner.objects.filter(name__iexact = str(name_kv)).count().
You are getting error because variable containing byte type data so converting byte data to string type may solve your problem.
I am very much a newbie to python programming I am trying to implement store procedure in flask.
This is the part of the code from where i am calling the stored procedure.
cursor.execute(insertquery)
database.commit()
cursor2.execute("select id from users where userid='"+username1+"'")
data2=cursor2.fetchall()
dictionary1=[dict(user=row[0]) for row in data2]
for data in dictionary1:
user_store=int((data.get('user')))
cursor2.callproc('insertPrediction',user_store)
database.commit()
database.close()
and this is the stored procedure
DROP PROCEDURE IF EXISTS `S3UploadDB`.`insertPrediction`;
PROCEDURE `S3UploadDB`.`insertPrediction`(IN id int(20))
BEGIN
INSERT INTO prediction(id,matchno,winner,points)
VALUES (id,1,'na',-500);
END;
when even i am invoking the procedure from toad it is working fine and the data is getting updated
but when ever it is getting invoked from the python code
TypeError: 'int' object is not iterable
It is failing at this line of the code
cursor2.callproc('insertPrediction',user_store)
I have converted the value to INT before passing it as the input to the stored procedure is of type int. I am using MySQL
Please help me on this error.
callproc requires a sequence of arguments such as a list or tuple, even if there is only one argument. Use:
cursor2.callproc('insertPrediction', [user_store])
Link to docs.
How to fixed error "how to fix invalid character 'x' in string escape code by py mongo"? My error like this
Check your BSON packet where there is X in it, you can query for the value x using the mongo find command if you already have in other mongo instance, it clearly shows it imported 14k records but some records can't be imported because of incompatible format. The solution query the db and find the error documents and remove it.
I have a value in orientdb, which is a JSON object.
Assume that JSON object is :
a = {"abc":123}
When I am sending a query using pyorient, it is not able to get this value in the select query, and hangs.
In the orientdb console, this JSON object seems to be converted in some other format like
a = {abc=123}
I guess it is hanging because of the same issue.
The query from pyorient is:
client.query("select a from <tablename>")
This hangs and doesn't seems to be working.
Could you please help ho to parse this JSON object in pyorient?
I used OrientDb's REST API service to fetch JSON object fields from the database. PyOrient hangs when requested for a JSON object field.
So fetch the rid of the record you want, and use REST services to get all the fields, which worked perfectly fine.
pyorient gives you the output something like:
a = {'abc': '123'}
and json.loads() function works with " and not with ', so to solve it, you need to do this:
b=str(a)
b.replace("'",'"')
json_data = json.loads(b)
print(json_data.keys())
I have defined a function to get vertex and after you get your vertex you can use for loop to parse the json result. Lets say the vertex "Root" have an attribute "name", and in the for loop after the query execution we can parse the value like "res.name" to fetch the value.
I think in recent version, they fixed the hanging issue. I am not facing any issue with hanging while query execution.
def get_verted(vertex):
result = client.command("select * from "+vertex)
for res in result:
print res.name
get_vertex("Root")