Using Spring-Data/MongoDB findBy/IsFalse with reserved keyword - spring-data-mongodb

I have a structure that looks kind of like this:
{
'foo': 'bar',
'isBaz': false
}
and the following Repository code
repository.findByIsBazIsFalse();
I figured that would only return records where isBaz is false, but it's also returning records where isBaz is set to true. My initial guess is that the field starts with the word 'is' which is a reserved keyword in the spring/mongo query.
FWIW I also tried via annotation but no luck
#Query("{isBaz:false}")
Does anyone know how to make it work without renaming the variable to 'baz'?

Make sure this field is defined as boolean and it should work!
private boolean isBaz;
repository.findByIsBazIsFalse();
[main] o.m.d.p.c :Sending command '{"find": "collection", "filter": {"isBaz": false}, ...}'

Related

ExtJS Issue with boolean data and grid column list filter as well as Ext.Data.Store

I am using ExtJS 6 (although from what I can tell it applies up to version 7.4 as well) and I have a grid with a booleancolumn xtype. For that boolean column I wanted to use the filter list option. Yes I know there is a boolean filter option however I don't like how it works using a radio button. I wanted to be able to select the Yes or No with checkboxes, however I found that only the option with true as the value worked. Here is my column config:
{
header: 'Active'
, dataIndex: 'inactive'
, xtype: 'booleancolumn'
, trueText: 'No'
, falseText: 'Yes'
, filter:{
type: 'list',
options: [[true,"No"],[false, "Yes"]]
}
}
This didn't work when excluding the 'options' property and letting it get the data from the store either by the way. After looking through the code I discovered that it takes the 'options' config and creates its own Ext.Data.Store using that as the data. See here as a simple example that can be run that will get the same issue:
var teststore = new Ext.data.Store({
fields: [
'id',
'text'
],
data: [[true,"No"],[false, "Yes"]]
});
The problem is that the 'false' boolean value is changed and is replaced with a dynamically created generic id. I discovered the issue lays in the constructor for 'Ext.data.Model' for the following line:
if (!(me.id = id = data[idProperty]) && id !== 0) {
If that line evaluates to true it will replace the id with the generated one. To fix this I just added ' && id !== false' to the end of the if statement and this fixed the issue.
I have not tested this fully, however the logic seems sound and it looks like the same type of issue occurred with the value of '0' hence the ' && id !==0'. Since we are directed here from the sencha forums I wanted to bring this up in case it helps someone.
Since my post already has the answer I will post a better way to do it other than directly changing the Ext code file (whether this is the proper way I may be wrong). Instead, you can create a new js file that will need to be included in your application (you can name it ext-overrides.js). In the new js file you need only type:
Ext.override(Ext.data.Model, {
constructor: function(data, session) {
.....
}
}
You would then copy the constructor function code from your version of the ExtJS code in where the '.....' is (if perchance the constructor function arguments are different you would have to update those as well) and just add the suggested change I made above in the 'question'. A search of the Extjs code for Ext.define('Ext.data.Model' should get you there easily and then just scroll to the constructor function.

And operator doesnt appear to work

I must be missing something,
my search includes the string long+beach so I expect results to only contain entries where (in this case) the location field contains both long and beach.
/indexes/person2/docs?$count=true&$top=100&search=long+beach&searchFields=location
plus
api-version=2014-07-31-Preview
the image I dont have rep to post shows miami beach, florida among others.
I tried setting &searchMode=any with no change.
the column looks like this
new { Name = "location",
Type = "Edm.String",
Key = false,
Searchable = true,
Filterable = true,
Sortable = true,
Facetable = true,
Retrievable = true,
Suggestions = true },
my bad?
Most likely this is an escaping issue. "+" is treated as a space character in most cases by code that handles URL encoding. "+" shouldn't show up literally in the URL but encoded as %2B. More in general, you should escape the search string before adding it to the search= parameter. Most languages have a built-in URL encoding primitive that will do this for you (e.g. Uri.EncodeDataString() in .NET)
I was having the same issue. %2B worked for me as well. I think I was thrown off by this page of the Azure Search documentation where it describes the simple query syntax that can be used to refine the search.
https://msdn.microsoft.com/en-us/library/azure/dn798920.aspx
It seems that the simple query syntax is what you would put in the search input box, not what you would send to the API.
Here's an example from my project:
search=Aberdeen%2Bdistrict (2 results)
search=Aberdeen+district (156 results)

Mongo query item from subarray, having trouble

I'm relatively new to mongodb, and I came into this company's set up that had the database already set up and running. It looks to me like the structure of this "array" isn't actually a proper array. They are saving info that I need in "offer_info_array" - which then has a nested array with an "offer id" which changes a lot between records, and then nested inside that is the info that I need to select. Here is an example of a record.
{
"_id" : ObjectId("52041af3bbf8057203000004"),
"offer_info_array" : {
"128" : {
"affid" : "68",
"s1" : "YJF"
}
},
"city" : "Cleveland",
"state" : "OH",
"zip" : "44111"
}
So from a whole db of records like this one, I need to find all records that have the "affid" of "68" - I realize this database is not structured correctly, but there's not much I can do about that for records that already exist. The "128" is the offer id that varies from record to record.
If anyone has any insight and can help me out with this, I would greatly appreciate it. Thank you!
You can use $where operator which accepts JavaScript function:
db.items.find({$where: function() {
for(var key in obj.offer_info_array)
if(obj.offer_info_array[key].affid == 68)
return true;
return false; }})
This function looks for properties of offer_info_array object and gets value of property by key. Then we verify if property value has affid property equal to 68. If yes, we return true which means objects matches our query. If there is no properties with affid equal to 68, we return false.
Keep in mind, that $where operator do not use indexes.

local variable 'results' referenced before assignment

I want to retrieve all indexes under elasticsearch index folder. I got this error.
UnboundLocalError at /tjobfucksearch/
local variable 'results' referenced before assignment
my views.py
from haystack.query import SearchQuerySet
def fucksearch(request):
query = request.GET.get('q', '')
if query:
results = SearchQuerySet().all()
return render_to_response("tjob/fucksearch.html", {
"results": results,
"query": query
})
my urls.py
url(r'^tjobfucksearch/$', 'tjob.views.fucksearch'),
Plus: haystack 2.0.0, django 1.4
Any advice would be appreciated. Plz help me.
Consider the case where no q parameter is provided. Then query is set to '', the if query condition fails, so results is not set (not even set to None; Python doesn't know about the name results at this point). So it fails when you try to get the value from results to pass it into the context dict for render_to_response. Perhaps add:
results = None
before:
if query:
....
This way, results will always be defined by the time you need to pass it to render. (You still have to handle the none-results case in your template!)

extjs store fail

ExtJS Model fields have mapping option.
fields: [
{name: 'brandId', mapping:'brand.id', type: 'int'},
{name: 'brandName', mapping:'brand.name', type: 'string'},
The problem is: if the response from server does not contain some field(brand field in my example) and mapping from inner fields is defined, Ext Store silently fails to load any records.
Does anybody have problems with this? Is it some kind of a bug?
UPDATE
To make it clear: suppose I have ten fields in my model. Response from server has nine fields, one is missing. If there is no nested mapping for this field (mapping:'x.y.z') everything is OK - store loads record, the field is empty. But if this field has to be loaded from some nested field and has mapping option - store fails to load ANYTHING.
UPDATE 2
I have found the code, that causes problems. The fact is: when Ext tries to load some field from Json it performs a check like this
(source["id"] === undefined) ? __field0.defaultValue : source["id"]
But when field has mapping option(mapping 'brand.id') Reader does it this way
(source.brand.id === undefined) ? __field20.defaultValue : source.brand.id
which causes error if source has no brand field.
In case you have same problems as I: you can fix it by overloading Ext.data.reader.Json's method createFieldAccessExpression
I agree that Ext should only fail to load that field, not the entire record. One option that isn't great, but should work, is instead use a mapping function:
{
name: 'brandId',
mapping: function(data, record) {
return data.brand && data.brand.id;
}
}
I could have the arguments wrong (I figured out that this feature existed by looking at the source code), so maybe put a breakpoint in there to see what's available if it doesn't work like this.
I think you're misinterpret mapping and nesting paradigms: these are not interchangeable.
If you define nesting in your data, the result MUST have the corresponding field.

Resources