Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
Cómo aplicar múltiples filtros en la colección mongodb según los criterios del usuario usando golang

Desde mi interfaz, los usuarios pueden aplicar filtros según el rango de fechas o el propósito, o ambos. ¿Cómo genero el filtro MongoDB para que si los usuarios pasan en el rango de fechas, solo lo use como filtro? Si es solo propósito, solo usa propósito y si es ambos, aplica ambos filtros. Estoy usando golang. Aquí está mi código

 var filterData map[string]interface{} if purpose != "" { filterData = bson.M{ "purpose": purpose, } var filterDate map[string]interface{} if //condition for date range{ filterDate = bson.M{ "paymentDate": bson.M{ "$gte": startdate, "$lt": enddate, }, } } //here i cant apply both cursor, err = collection.Find(conn.DatabaseContext, findQuery)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Utilice un solo mapa para el filtro (por ejemplo, bson.M ) y solo agregue elementos para las condiciones de filtro que proporcionó el usuario.

Por ejemplo:

 var purpose string var startDate, endDate time.Time filter := bson.M{} if purpose != "" { filter["purpose"] = purpose } if !startDate.IsZero() && !endDate.IsZero() { filter["paymentDate"] = bson.M{ "$gte": startDate, "$lt": endDate, } } cursor, err = collection.Find(ctx, filter)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!