how to rewrite mongo shell code to regular python file? - python

The below code exist inside a python file I run at cli as 'python test.py '....
import pymongo
from pymongo import Connection
connection = Connection('localhost', 27017)
db = connection.school
data = db.students.aggregate(
{ $match : { 'scores.type': 'homework' },
{ $project: { id : $_id,
name : $name,
scores : $scores
},
{ $unwind: "$scores" },
{ $group : {
_id : "$id",
minScore: { $min :"$scores.score" },
maxScore: { $max: "$scores.score" }
}
});
for _id in data:
print _id
# NOTE: this can only be done ONCE...first find lowest_id then
# remove it by uncommenting the line below...then recomment line.
# db.students.remove(data)
when I run this code I get this error...
File "test.py", line 11
{ $match : { 'scores.type': 'homework' },
^
SyntaxError: invalid syntax
How do I rewrite this code so it works correctly from inside my test.py python file?

You have a few syntax issues.
First, pipeline is an array (a list in Python) where you are trying to pass multiple pipeline elements as separate parameters.
Second, you need to quote the pipeline operators, such as $match: '$match'
Here is a page that has some nice examples:
http://blog.pythonisito.com/2012/06/using-mongodbs-new-aggregation.html

Related

How to fix 'parse error on (VAR_SIGN)' in a graphql query in python

I am having trouble with GraphQL queries made in python. It says that the signal $ that determine a variable in the query cannot be parsed.
Error message:
{"errors":[{"message":"Parse error on \"$\" (VAR_SIGN) at [3, 3]","locations":[{"line":3,"column":3}]}]}
Is there other way to use variables in this kind of request?
Here is my query, I didn't paste the fragment because I think it's not the problem
query ApplicationIndexQuery(
$status: Boolean!
$page: Int
$perPage: Int
$filters: ApplicationFilter
$sort: String
) {
allOpportunityApplication(page: $page, per_page: $perPage, filters: $filters, sort: $sort) {
...ApplicationList_list
}
}
variables = {
"status": True,
"page": 1,
"perPage": 517,
"filters": {
"date_realized": {
"from": "2018-12-01",
"to": "2019-03-31"
},
"person_home_mc": 1535,
"programmes": 5
}
query should be at the top level, but it seems like in your example it's enclosed in curly braces. See below:
https://github.com/apollographql/graphql-tag/issues/180#issuecomment-386540792

Highest value(max) from mongodb collection error - Flask Python

I am trying to get the highest value for nr from a mongo database collection named prob that looks like this for now:
{
"_id": {
"$oid": "5ae9d062f36d282906c59736"
},
"nr": 1,
"nume": "numeunu",
"enunt": "-",
"datedeintrare": "-",
"datedeiesire": "-"
}
{
"_id": {
"$oid": "5ae9f190f36d282906c5b461"
},
"nr": 2,
"nume": "numedoi",
"enunt": "-",
"datedeintrare": "-",
"datedeiesire": "-"
}
My testing flask code is:
#app.route('/test')
def testaree():
prob = mongo.db.prob
return prob.find().sort({"nr":-1}).limit(1)
but it gives me an error TypeError: if no direction is specified, key_or_list must be an instance of list
The syntax for sorting using pymongo is different from the mongo query language.
In the above code the expression used is
sort({"nr":-1})
However , that is a mongo shell query syntax. When using pymongo it should be
sort("nr", pymongo.DESCENDING)
You could also use find_one instead of a limit clause once the data is sorted.
db.prob.find_one(sort=[("nr", pymongo.DESCENDING)])

$eq invalid syntax Mongo DB

I have a nested json and i want to find a record whose value is equal to a given number. I'm using pymongo in python wih equal operator but getting some errors:
from pymongo import MongoClient
import datetime
import json
import pprint
def connectDatabase(service):
try:
if service=="mongodb":
host = 'mongodb://connecion_string.mlab.com:31989'
database = 'xxx'
user = 'xxx'
password = 'xxx'
client = MongoClient(host)
client.xxx.authenticate(user, password, mechanism='SCRAM-SHA-1')
db = client.xxx
new_posts = [{"author": "Mike", "text": "Another post!",
"tags": [
{
"CSPAccountNo": "414693"
},
{
"CSPAccountNo": "349903"
}]
}]
result=db.posts1.insert_many(new_posts)
print (xxx.posts1.find( {"CSPAccountNo": { $eq: "414693" } } )
except Exception as e:
print(e)
pass
print (connectDatabase("mongodb"))
Error:
File "mongo.py", line 35
print (cstore.posts1.find( {"CSPAccountNo": { $eq: "414693" } } )
^
SyntaxError: invalid syntax
I'm very new to Mongodb. Any help would be appreciated
In Python you have to use Python syntax, not JS syntax like in the Mongo shell. That means that dictionary keys need quotes:
print(cstore.posts1.find( {"CSPAccountNo": { "$eq": "414693" } } )
(Note, in your code you never actually insert the new_posts into the collection, so your find call might not actually find anything.)
It should be
print (xxx.posts1.find( {"tags.CSPAccountNo": { $eq: "414693" } } )

Converting MongoDB aggregation pipeline code from Javascript to Python

As a newbie in Python/MongoDB/pymongo, I used Studio 3T for generating a working aggregation pipeline for accessing and processing the content of a mongoDB collection. I can see that it works perfectly fine there and outputs the expected result. The problem is that Studio 3T generates a .js file for the code while I need to write the code in Python within a wrapper.
Code executing correctly on Studio 3T:
db.cln_matching_results.aggregate(
// Pipeline
[
// Stage 1
{
$unwind: {
path : '$_availability',
includeArrayIndex : 'arrayIndex', // optional
preserveNullAndEmptyArrays : false // optional
}
},
// Stage 2
{
$unwind: {
path : '$_availability.availability_data',
includeArrayIndex : 'arrayIndex', // optional
preserveNullAndEmptyArrays : false // optional
}
},
// Stage 3
{
$match: {
'_availability.availability_data.start_date': {$gte: "2017-09-14T00:00:00.000Z", $lte: "2017-09-31T00:00:00.000Z"}
}
},
// Stage 4
{
$group: {
_id : '$_id',
MD_offered_max: { $sum: { $divide: [ "$_availability.availability_data.value", 100 ] } }
}
},
]);
My attempt at replicating the working code within my Python wrapper breaks the pipeline and gives an empty array after execution. I stress that the rest of the python code works perfectly fine when I comment out the piece of code below:
test_pipeline.extend([
{"$unwind": {'path': '$_availability', 'preserveNullAndEmptyArrays': False}},
{"$unwind": {'path': '$_availability.availability_data', 'preserveNullAndEmptyArrays': False}},
{'$match': {'$_availability.availability_data.start_date': {'$gte': "2017-09-14T00:00:00.000Z", '$lte': "2017-09-16T00:00:00.000Z"}}},
{'$group':{'_id' : '$_id', 'MD_offered_max': { '$sum': { '$divide': [ "$_availability.availability_data.value", 100 ] } }}},
{'$addFields': {'availability_scoring': '$MD_offered_max'}}
])
My question is: where did I go wrong in my attempt at translating the code from a .js format to Python? Thanks for your help.

How do I properly construct a query using the elasticsearch python API?

I've got some code that looks like this
from elasticsearch import Elasticsearch
client = Elasticsearch(hosts = [myhost])
try:
results = es_client.search(
body = {
'query' : {
'bool' : {
'must' : {
'term' : {
'foo' : 'bar',
'hello' : 'world'
}
}
}
}
},
index = 'index_A,index_B',
size = 10,
from_ = 0
)
except Exception as e:
## my code stops here, as there is an exception
import pdb
pdb.set_trace()
Examining the exception
SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;
And further down
Parse Failure [Failed to parse source [{"query": {"bool": {"must": {"term": {"foo": "bar", "hello": "world"}}}}}]]]; nested: QueryParsingException[[index_A] [bool] query does not support [must]];
The stack trace was huge so I just grabbed snippets of it, but the main error appears to be that "must" is not supported, at least the way I have constructed my query.
I was using this and this for guidance on constructing the query.
I can post a more complete stack trace, but I was hoping someone is able to see a very obvious error that I have made inside the "body" parameter inside the "search" method.
Can anyone see anything that I have clearly done wrong as far as constructing the query body for the python API?
The syntax of the query doesn't look correct to me. Try this:
results = es_client.search(
body = {
"query": {
"bool": {
"must": [
{
"term": {
"foo": {
"value": "bar"
}
}
},
{
"term": {
"hello": {
"value": "world"
}
}
}
]
}
}
},
index = 'index_A,index_B',
size = 10,
from_ = 0
)

Categories