Closed hamza-maqsood closed 2 years ago
ensureIndex
or createIndex
do not create index if it does exist (but the existence of the index is checked on server side). So yes you can write this code, especially if you use DI and if LocationRepositoryImpl
is a singleton.
By the way, the use of GlobalScope is not recommended, it would be better to setup all your collections in a suspend function, called at application startup.
HTH
yeah I thought about GlobalScope and I put it just to test this. Actually I am thinking of doing something like this:
override suspend fun provideUserLocationCollection(): CoroutineCollection<UserLocation> {
val collection: CoroutineCollection<UserLocation> = db.getCollection<UserLocation>()
collection.ensureIndex(Indexes.geo2dsphere("location"))
return collection
}
thanks that helps, and yes, LocationRepositoryImpl
is a singleton and comes from DI
I couldn't find any documentation on geo2dsphere index on this link.
Right now, I am trying to create the index as follows
this is my model class
And then in the repository impl, I am creating the index as follows
Now my questions is, do I need to call this ensureIndex or createIndex method every time the collection is initialized, or what's the correct way to do it?