I have a MongoDB query:
db.list.find({categories:{$elemMatch:{ "$regex":".*Bar.*", $not:/^Barbeque/}}}).pretty()
where it looks at the elements in the categories array and I think gets all documents where there is an element that contains "Bar" but none that contain "Barbecue". How to I check make sure that my query is correct?
Let me know if my query is wrong and how I could fix it.
You should specify the end of string like this:
$not:/^Barbeque$/
rather than
$not:/^Barbeque/
because if any of file contains (i.e. "Barbequee"), it must return to your query result.
about make sure that my query is correct or not, i have no idea. But if your query is correct, mongo must return the value that matches the written query.
if something goes wrong, it must be that the logic in the query that results in mongo returns an unexpected value for you.
so check your query before you run it. :D
Related
Trying to figure out the right way to parse key-value pairs produced by a Cypher query:
#app.route('/about')
def about():
data = graph.run("MATCH (n) RETURN n.level")
for record in data:
return render_template("output.html",output=record)
Please disregard the fact that I'm not combining the returned records into a list prior to populating the template. I do get one record as output, and am ok with that for now.
What I'm struggling with is - how do I handle the resulting k/v pair
(u'n.level': u'high')
I mean, if I'm just interested in the value 'high', is there a clean way to get hold of it?
Sorry if this sounds too basic. I do understand, there must be some parsing tools, but at this point, I just don't know where to look.
Sorry, the solution is simple. Flask returns a py2neo.database.record object, which can be indexed just like a list, the only caveat being that the list has only one element (not two, as it might appear).
So, if variable record above equals to (u'n.level': u'high'), record[0] will be equal to 'high'.
And the 'u's can be just ignored altogether, as explained elsewhere on SO.
I'm seeing very bizarre behavior with the REGEXP_MATCH function in google big query. The function appears to work perfectly fine for public data but is not working on my dataset. I have a dataset imported from the csv with the first two lines (first is header row which all becomes the schema where everything is a string), there's a lot more but the following is the only relevant data for this case.
"id","common_name","botanical_name","low_hardiness_zone","high_hardiness_zone","type","exposure_min","exposure_max","moisture_min","moisture_max"
"plant1","Abelia","Abelia zanderi 'Conti (Confetti)'","5b","9a","Shrub","Partial Sun","Full Sun","Dry","Dry"
When I run the query:
SELECT * FROM [PlantLink_Plant_Types.plant_data_set]
WHERE REGEXP_MATCH('common_name',r'.*')
I get every result.
However, when I run the query:
SELECT * FROM [PlantLink_Plant_Types.plant_data_set]
WHERE REGEXP_MATCH('common_name',r'A.*')
I get no results, which is really weird because the plant common name Abelia starts with an A.
Now my regex magic is not that strong, but I am pretty sure the pattern is not at fault. Additionally I've run the public dataset test queries with REGEXP_MATCH and they run correctly. Does anyone have any clue why REGEXP_MATCH would not always function as advertised?
Note:
REGEXP_MATCH('common_name',r'.*') matches the string 'common_name'
while
REGEXP_MATCH(common_name,r'.*') matches a field in your table that is called common_name
the 1st one is always true and therefore you get all results.
I guess you wanted to refer the content of the field, so you need to use the second one.
REGEXP_MATCH(common_name,r'A.*') should return all records that field common_name contains "A".
hope this helps.
Issue is the string 'common_name' does not start with 'A'.
Check this:
REGEXP_MATCH('common_name',r'.*'): All results.
REGEXP_MATCH('common_name',r'A.*'): No results.
REGEXP_MATCH('common_name',r'c.*'): All results.
REGEXP_MATCH(common_name,r'A.*'): All results that somewhere have an 'A'.
:)
Does anyone know if there is a way to query MongoDB and have only certain fields returned by using a regex as part of the projection?
For example: Given a collection having arbitrary field names, how might I query the collection and only return field names matching the regex '^foo'.
Possibly something like this?
db.mycollection.find({},{$regex:"^foo"})
Thanks.
Brent.
I think you need to break down the process into two pieces, the first one is retrieving the fields names from MongoDB.
Then the second piece is that you can run the regex on the result, and from there you can query the DB with the right fields.
In SQL Alchemy, an instance of a Query class is returned from each session query made.
I tried to evaluate the query like this, expecting it to behave in a similar way to a list:
if session.query(...).filter_by(...):
However it always evaluated to true, so I ended up doing this instead:
if session.query(...).filter_by(...).count():
Which seems a bit long-winded and incorrect. Is there a better way to check whether a query will return results?
query is necessarily conservative about when it emits SQL, which is why for example it has no __len__() method.
typically if I want to see if a query returns results, I call .first(), so that at least the DB only needs to find one row rather than all of them in order to determine a .count().
I would like to do something like:
App.Model.find({unique_attribute_a: 'foo'}).objectAt(0).get('attribute_b')`
basically first finding a model by its unique attribute that is NOT its ID, then getting another attribute of that model. (objectAt(0) is used because find by attribute returns a RecordArray.)
The problem is App.Model.find({unique_attribute_a: 'foo'}).objectAt(0) is always undefined. I don't know why.
Please see the problem in the jsbin.
It looks like you want to use a filter rather than a find (or in this case a findQuery). Example here: http://jsbin.com/iwiruw/438
App.Model.find({ unique_attribute_a: 'foo' }) converts the query to an ajax query string:
/model?unique_attribute_a=foo
Ember data expects your server to return a filtered response. Ember Data then loads this response into an ImmutableArray and makes no assumption about what you were trying to find, it just knows the server returned something that matched your query and groups that result into a non-changable array (you can still modify the record, just not the array).
App.Model.filtler on the other hand just filters the local store based on your filter function. It does have one "magical" side affect where it will do App.Model.find behind the scenes if there are no models in the store although I am not sure if this is intended.
Typically I avoid filters as it can have some performance issues with large data sets and ember data. A filter must materialize every record which can be slow if you have thousands of records
Someone on irc gave me this answer. Then I modified it to make it work completely. Basically I should have used filtered.
App.Office.filter( function(e){return e.get('unique_attribute_a') == 'foo'}).objectAt(0)
Then I can get the attribute like:
App.Office.filter( function(e){return e.get('unique_attribute_a') == 'foo'}).objectAt(0).get('attribute_b')
See the code in jsbin.
Does anyone know WHY filter works but find doesn't? They both return RecordArrays.