Firestore query date range with using 'where' - javascript

I need to grab doc from collection if its today and status true, but it seems not working is there any better solution for this query?
My query:
var query = firebase.firestore().collection("book")
query = query.where('completed', '==', true)
query = query.orderBy('publishdate').startAt(1555567200000).endAt(1555653599999)
query.get().then(...)
I've also tried:
var query = firebase.firestore().collection("book")
query = query.where('completed', '==', true)
query = query.where('publishdate', '>', 1555567200000)
query.get().then(...)
but no result

I had to do two things to get this working:
Create a composite index by clicking on the link that showed up in my JavaScript console.
Pass in the timestamp as a proper Date() instead of a number: query = query.orderBy('publishdate').startAt(new Date(1555653600000));
With that it works fine for me. See a working sample here: https://jsbin.com/gufogib/edit?js,console

Related

Firestore query: how to filter by two fields

I want to filter documents by their location (using geohash) but I also want to order them by timestamp (to get the latest documents first). The following query fetches the nearby documents but they are not ordered by timestamp.
DocumentSnapshot<Object?>? lastFetchedDoc; // used for pagination
Future<void> getReviews() async {
Query<Map<String, dynamic>> query = firestore
.collectionGroup("reviews")
.where("city", whereIn: ['New York', 'Philadelphia', 'Washington'])
.orderBy('geohash')
.orderBy('timestamp', descending: true)
.where('geohash', isGreaterThanOrEqualTo: "dph")
.where('geohash', isLessThanOrEqualTo: "dp~")
.limit(10);
if (lastFetchedDoc != null) {
query = query.startAfterDocument(lastFetchedDoc!);
}
QuerySnapshot snapshot = await query.get();
lastFetchedDoc = snapshot.docs.last;
}
How can I create a query that fetches the nearby documents ordered by timestamp?
Thanks
Your query results will first be ordered by geohash and only then on timestamp, meaning that the value of the timestamp field only affects the order of the results for documents where the value of geohash is the same.
There is no way to change this behavior for range queries such as the one you're doing. If you want to further process the documentation in order of timestamp, you will have to reorder them in your application code.

Multiple query using QueryTask ArcgGIS JS Api 3

I am using QueryTask in ArcgGIS JS Api 3 and I want to execute multiple query in one call, I took a look at the documentation and I didn't find the possibility to do it
this is what I am using
arraygraphic.map(async (e)=>{
let query = new esri.tasks.Query();
query.where = "id = '"+id+"'";
query.outFields = ["*"];
let response = await queryTask.execute(query)
})
and this is what I want to do
let queryList=[];
arraygraphic.map(async (e)=>{
let query = new esri.tasks.Query();
query.where = "id = '"+id+"'";
query.outFields = ["*"];
queryList.push(query)
})
let response = await queryTask.execute(queryList)
any suggestion or help ?
thank you in advance
You can only have one "Query", but you can create a better where statement that searches for all the "id" in a single where statement.
Your where statement would end up looking something like:
id in (1,2,3,4).
You can find more details about the supported SQL under "Request parameters" at https://developers.arcgis.com/rest/services-reference/enterprise/query-feature-service-layer-.htm#GUID-62EE7495-8688-4BD0-B433-89F7E4476673
If you're not able to put all the queries in one SQL statement as Bjorn suggested (e.g. queries to multiple layers), then you can add the requests to an array and use promise.all to send all the requests
https://developers.arcgis.com/javascript/3/jssamples/query_deferred_list.html

How do I prevent sql injection on a WHERE IN request with parameterized query? [duplicate]

I am trying to workout how to pass in a comma sepated string of values into a postgres select within in clause query using node-postgres but unfortunately can't seem to get it working and can't find any examples.
I have the following code:
function getUser() {
let inList = "'qwerty', 'qaz'";
const result = await pgPool.query("SELECT name FROM my_table WHERE info IN ($1)", [inList]);
if (result.rowCount == 1) return result.rows.map(row => row.name)
return false
}
Basically want the end result query to process:
SELECT name FROM my_table WHERE info IN ('qwerty', 'qaz')
Unsure if this is actually the correct way of doing this with pgPool?
The right way is in the FAQ on Github --> https://github.com/brianc/node-postgres/wiki/FAQ#11-how-do-i-build-a-where-foo-in--query-to-find-rows-matching-an-array-of-values
You should be able to do it that way :
pgPool.query("SELECT name FROM my_table WHERE info = ANY ($1)", [jsArray], ...);

Display Firebase data appended with jquery to a table in order

I am currently trying to append data through I pull from Firebase to a table in the order of newest to oldest posts. I currently have the following setup in my code base (simplified to address issue):
var theDataRef = new Firebase('https://my-app.firebaseio.com');
theDataRef.orderByChild("timestamp").limitToLast(25).on('child_added', function (snapshot) {
var message = snapshot.val();
displaytableRow(message.name, message.text);
}
function displaytableRow(name, message) {
$("#sch").find('tbody > tr:first')
.before($("<tr><td><div>" + name + ":" + message + "</div></td></tr>"))
};
I have tried to create a table that displays newest to oldest data by using both firebase and jquery techniques but every time my data is displayed in a random order. I have a working timestamp field on every record in my data as well but even ordering by that does not solve the problem. Has anybody had any experience building this successfully?
EDIT:
The timestamp is gotten with the following code:
var timestamp = Firebase.ServerValue.TIMESTAMP;
And the database architecture is structured like so:
If you want to order your posts from newest to oldest you can do the following:
1/ Store a field in your post that is the inverse of the TimeStamp as follows:
var tDate = new Date().getTime();
var postData = {
//.....
timestampInverted: (0 - tDate),
//.....
};
2/ Query your posts ordered by this field as follows:
theDataRef.orderByChild("timestampInverted").limitToLast(25).once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
displaytableRow(childSnapshot.val().name, childSnapshot.val().text);
});
});
It is important to note that theDataRef.orderByChild("timestampInverted").limitToLast(25) returns a Query, which returns a DataSnapshot : Therefore you need to use snapshot.forEach() (see doc here) to iterate over the different posts items.
Also note that "even when there is only a single match for the query, the snapshot is still a list; it just contains a single item. To access the item, you always need to loop over the snapshot".

And Query on Parse Cloud code

this looks a very trivial query but I can't seem to make it work..
I am trying to get for instance an object that has locationName = "NYC" AND groupName = "Adults". I am trying to query using this code I found on Parse documentation:
var groupQuery = new Parse.Query("MyTable");
groupQuery.equalTo("group", groupName);
var locationQuery = new Parse.Query("MyTable");
locationQuery.equalTo("location", locationName);
var mainQuery = Parse.Query.or(locationQuery, groupQuery);
But I obviously fail because I am using Parse.Query.or instead what should have been Parse.Query.and which, for some reason doesn't exist...
Is there any alternative way to do it, for some reason I cannot find it on the documentation..
Cheers
Parse queries use and by default, which is why there is only a Parse.Query.or().
What you want to do can simply be achieved this way :
var mainQuery = new Parse.Query("MyTable");
mainQuery.equalTo("group", groupName);
mainQuery.equalTo("location", locationName);

Categories

Resources