0

I have been looking at the documentation but I can't seem to figure out how to correctly create a filter object for the buildfire.datastore.search query.

I have an address property on my object and I want to be able to type in a partial amount of the address and have it return. Below are the filter objects I have tried to pass into the search query:

search = {filter: {"$json.address": {"$regex": `/${this.state.search}/`}}};

search = {filter: {'$regex': {'$json.address': this.state.search}}};

Neither have worked. End goal is:

buildfire.datastore.search(search, 'location', cb);

EDIT:

I even tried to hardcode the regex in the docs:

"$or" : [
  {"description": {"$regex":"/new /"}}
]

and it didn't work (I replaced 'new' with a string I knew would show).

user2465134
  • 8,793
  • 5
  • 32
  • 46

1 Answers1

1

I just inserted the following on the control side:

for(let i = 0 ; i < 50 ; i++) {
    buildfire.datastore.insert({
        name: "Address" + i
        ,address: i + " " + (i % 2 ? "Main ":"4th ") + (i % 3 ? "ave":"st" )
    },function(){});
}

then did a search on the widget side like this:

<body>
<input type="text" id="criteria" /><button onclick="search()">Search</button>
<div id="results"></div>
<script>
    function search(){
        var cri = document.getElementById("criteria").value;

        buildfire.datastore.search( {filter:{"$json.name": {"$regex": cri  }  } } , function(err,results){
            document.getElementById("results").innerHTML = JSON.stringify(results);
        });
    }
</script>
</body>

Works fine. Given if you want the search to be more complex then you need to modify the regex statement for example case insensitivity.

Hope this helps

Daniel_Madain
  • 491
  • 4
  • 9
  • The documentation shows that you must, or at least it seems like you must, put forward slashes around your search parameter. In the Mongo docs they do that as well. However, in your example, and what now works on my project, you simply just pass a string. The forward slash will ruin your search – user2465134 Aug 23 '17 at 18:38
  • first off `DataStore` is not exactly mongoDB. However, it is build in a similar way. Tat being said the slashes are just to indicate if you are using Regex or not. for example: `prop:"text"` means `prop` must be exactly "text". However, `prop: /text/g` means you want it to *contain* "text". In SQL it would be `prop ="text"` vs `prop like "%text%"` – Daniel_Madain Aug 24 '17 at 21:18