D
D
Denis Sokolov2022-04-12 15:05:41
MongoDB
Denis Sokolov, 2022-04-12 15:05:41

How can I get 1 from a query and not an array?

Here are my structures

type Result struct {
  User    primitive.ObjectID `json:"user" bson:"user"`
  Test    primitive.ObjectID `json:"test" bson:"test,omitempty"`
  Answers []Answers          `json:"answers" bson:"answers"`
}

type Test struct {
  Id    primitive.ObjectID `json:"id" bson:"_id,omitempty" db:"_id"`
  Title string             `json:"title" bson:"title" db:"title" binding:"required"`
  Tasks []Task             `json:"tasks" bson:"tasks" db:"tasks" binding:"required"`
}

I make this request and it gives me an error
error decoding key test: cannot decode array into an ObjectID

matchStage := bson.D{{"$match", bson.D{{"user", objectID}}}}
  lookupStage := bson.D{{
    "$lookup", bson.D{
      {"from", "tests"},
      {"localField", "test"},
      {"foreignField", "_id"},
      {"as", "test"},
    }},
  }

  cursor, err := r.db.Aggregate(nil, mongo.Pipeline{matchStage, lookupStage})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question