Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To Create a 2dsphere index on geojson object field #305

Open
ganeshagrawal-fl opened this issue Oct 27, 2023 · 1 comment
Open

How To Create a 2dsphere index on geojson object field #305

ganeshagrawal-fl opened this issue Oct 27, 2023 · 1 comment
Labels
question Further information is requested

Comments

@ganeshagrawal-fl
Copy link

I am trying these 2 ways to create a 2dsphere index on a collection, but both failed. Both created a regular index.
The first creates a compound index and the second creates a regular index.

import (
	"context"

	"github.com/paulmach/orb/geojson"
	"github.com/qiniu/qmgo/options"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

// StoreGeoJSONCollection is the collection name for the store geojson model
const StoreGeoJSONCollection = "stores_geojson"

// StoreGeoJSON represents the store geojson model
type StoreGeoJSON struct {
	Model `bson:",inline"`

	StoreId           ObjectID                   `bson:"store_id"            json:"store_id"            validate:"required"`
	StoreName         string                     `bson:"store_name"          json:"store_name"          validate:"required"`
	Title             string                     `bson:"title"               json:"title"               validate:"required"`
	Geojson           *geojson.FeatureCollection `bson:"geojson"             json:"geojson"             validate:"required"`
	DeliveryFee       float64                    `bson:"delivery_fee"        json:"delivery_fee"`
	MinimumOrderValue float64                    `bson:"minimum_order_value" json:"minimum_order_value"`
	DeliveryProductId *string                    `bson:"delivery_product_id" json:"delivery_product_id"`
}

// CollectionName returns the collection name for the store geojson model
func (StoreGeoJSON) CollectionName() string {
	return StoreGeoJSONCollection
}

// Indexes returns the indexes for the store geojson model
func (StoreGeoJSON) Indexes() []options.IndexModel {
	return []options.IndexModel{
		{
			Key:          []string{"geojson.features.geometry", "2dsphere"},
			IndexOptions: Index().SetSphereVersion(3),
		},
		{
			Key:          []string{"geojson.features.geometry"},
			IndexOptions: Index().SetSphereVersion(3),
		},
	}
}

Indexes Screenshot

image

Please share the correct way to create 2dsphere indexes using qmgo.

@jiangz222
Copy link
Collaborator

Hi, please share your complete codes to create index!

@jiangz222 jiangz222 added the question Further information is requested label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants