Hello, I got an error when I use a pipeline with geoNear to count the documents in db. Here is the code:
func createGeoStage(lng float32, lat float32, radius int32) (jsonStage string) {
geoStage := `
{
"$geoNear":{
"includeLocs":"location",
"distanceField":"distance",
"near":{
"type":"Point",
"coordinates":[ %g, %g]
},
"maxDistance": %v,
"spherical":true
}
}`
geoStage = fmt.Sprintf(geoStage, lng, lat, radius)
return geoStage
}
func createMatchStage(parUserID string) (jsonStage string) {
if id, err := primitive.ObjectIDFromHex(parUserID); err == nil {
matchStage := `
{
"$match":{
"$and":[
{
"$or":[
{
"proprietary":false
},
{
"user_id": {"$oid": %q}
}
]
},
{
"status":"active"
}
]
}
}`
matchStage = fmt.Sprintf(matchStage, id)
return matchStage
} else {
return `
{
"$match":{
"$and":[
{
"proprietary":false
},
{
"status":"active"
}
]
}
}`
}
}
func BuildPipelineFromJsons(logger log.RootLogger, jsonStages ...string) (mongo.Pipeline, error) {
var pipeline mongo.Pipeline
for idx, jsonStage := range jsonStages {
var stage bson.D
if err := bson.UnmarshalExtJSON([]byte(jsonStage), false, &stage); err != nil {
return nil, errors.BadRequest("syntax error", fmt.Sprintf("error unmarshalling stage %d: %v", idx, err))
}
pipeline = append(pipeline, stage)
}
return pipeline, nil
}
The code call the functions above:
geoStage := createGeoStage(2.3522, 48.8566, 1000)
matchStage := createMatchStage("") //no user id
countStage := `
{
"$count":"number"
}`
//here to prepare the pipeline to counting the places
counting, err := dboperate.BuildPipelineFromJsons(logger, geoStage, matchStage, countStage)
if err != nil {
return err
}
cur, err := collection.Aggregate(ctx, counting, options.Aggregate()) -> here I get the error:
errorDetails":"(Location16604) geoNear command failed: { ok: 0.0, errmsg: \"error processing query: ns=db.place limit=100Tree: GEONEAR field=location maxdist=1000 isNearSphere=1\nSort: {}\nProj: { $pt: { $meta: \"geoNearPoin...\", code: 2, codeName: \"BadValue\" }"
can anyone help to check the code? thanks a lot!
James