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

340
Views
How to write mongodb shell query into golang

I have a mongodb query that I written for shell which is working fine but now I want to transfer it into golang below is the query and solution that I tried

db.getCollection('test').find({"course" : /^ba$/i, "status":{"$ne":0}}).count()

That I tried:-

query := bson.M{"status": bson.M{"$ne": 0}, "course": "/^" + parameter + "$/i"}
query := bson.M{"status": bson.M{"$ne": 0}, "course": `/^` + parameter + `$/i`}

but not working fine can anyone please find mistake that I'm doing.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is how you could count documents and use regular expressions with golang mongodb driver.


pattern := "test"
count, err := coll.CountDocuments(ctx, bson.D{
    {"course", primitive.Regex{Pattern: "^" + pattern + "$", Options: "i"}},
    {"status", bson.D{{"$ne", 0}}},
})
if err != nil {
    panic(err)
}
fmt.Println(count)
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!