geofirestore-js icon indicating copy to clipboard operation
geofirestore-js copied to clipboard

Feature request: add fieldname to near function

Open SchnMar opened this issue 3 years ago • 6 comments

Hello Michael,

first of all, thank you for this amazing package. I'm trying to use it in a cloud function for my flutter app. To be able to use it, I need an extension of the near function.

In my flutter geofire I use a "within"-function, which has 3 parameters:

  • center (as in js-near function)
  • radius (also as in js-near function)
  • optional field

The third optional parameter is what I need. In this parameter, I can pass a field name for a given field structure within my document. This structure contains the location data required by Geofire.

Can you extend the library or is there already a possibility within the given library for this functionality?

Best, Marcel

SchnMar avatar Sep 26 '20 20:09 SchnMar

@SchnMar could you share a very simple example of what you mean? Or an example of the data structure created by the flutter library you're using?

MichaelSolati avatar Sep 26 '20 21:09 MichaelSolati

@MichaelSolati Sure, thank you for your quick reply.

I have a collection called "user", which contains a field location, which contains location data created by my flutter app.

Bildschirmfoto 2020-09-26 um 20 13 31

Now, I want to query all users within a given area using GeoFirestore. I need to do this in a cloud function as I want to send a push notification to users within an area.

As mentioned geoflutterfire has a within-function for these cases, which includes a fieldname parameter. Maybe that's a solution in JavaScript as well? (https://pub.dev/packages/geoflutterfire)

Thank you, Marcel

SchnMar avatar Sep 27 '20 10:09 SchnMar

Any news on this? Would love to hear back from you @MichaelSolati. Thank you

SchnMar avatar Oct 08 '20 18:10 SchnMar

@MichaelSolati I'm also wondering for the same reasons but to use your library server-side. The core need is to flexibly make a query based on geolocation data nested under a property/field of the document. This is done by GeoFireX as seen here. With this improvement, you could have many GeoPoints and their geohashes per document to store and query by, which is actually a reason I went to use GeoFireX for a project instead of this library because you can't flexibly store more than a single field named "coordinates" with GeoFirestore.

The current issue with GeoFireX is that you can't use it with Firebase >8.0.0. If you could add this to GeoFirestore it would bring in all the unhappy developers who tried using GeoFireX but couldn't.

dylangolow avatar Jan 14 '21 06:01 dylangolow

Have you looked at the GeoCollectionReference:constructor(), where a _customKey can be defined ? If I read it right, this seems to be the feature you are requesting. Should be there since 4.2.0 (2020-07-19).

Not sure if it will work in a nested way, but the dot notation of nested field names (as a string) is supported in firebase e.g (obj['location.position'] instead of obj.location.postition ). You might want to give that a try.

import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
import * as geofirestore from 'geofirestore';
 
firebase.initializeApp({
  // ...
});
 
const firestore = firebase.firestore();
const GeoFirestore = geofirestore.initializeApp(firestore);
const geocollection = GeoFirestore.collection('users', 'location.position'); // Dot notation as string

const query = geocollection.near({ center: new firebase.firestore.GeoPoint(40.7589, -73.9851), radius: 1000 });
 
query.get().then((value) => {
  console.log(value.docs);
});

Of course if that doesn't work then your request would be a nice to have, but it think it is simple(r) to catch it on the implementation side. By creating a serverside trigger (or implement it client side), where the nested location data is also written to a under_scored_field on the top level e.g :

"users" : { 
	[{uid}]:{
		[..]
		"location" : {
				[..]
				"position" : {[2]}
				}
		// Start duplicated data
		"location_position" : {
			 "geohash": string;
			 "geopoint": GeoPoint;

		}
}

spoxies avatar Aug 31 '22 11:08 spoxies

@spoxies I was under the impression this was to be able to index a document with many fields. So like, a user can have a home location and a work location, and you could query either one.

Though you are right, you can set the property you want to index in the constructor of a GeoCollection and index that field.

MichaelSolati avatar Aug 31 '22 20:08 MichaelSolati