Я использую Монго для хранения данных в базе данных с Go.Итак, есть две коллекции с именем section и fields.коллекция разделов содержит такой документ, как:
{ "_id" : 2, "name" : "Message", "status" : 1 }
, а коллекция полей содержит документы, как показано ниже: -
{
"_id" : 6,
"lead_section_id" : 2,
"help_text" : "This is a tool tip",
"name" : "Test11",
"status" : 9
}
{
"_id" : 7,
"lead_section_id" : 2,
"help_text" : "This is a tool tip",
"name" : "Test11",
"status" : 1
}
см. документы коллекции полей, содержащие lead_section_id is 2
во время извлечения записейиспользуя $lookup
агрегацию, то он вернет оба документа в коллекции полей, в то время как я передаю статус, равный 0,1 в запросе. В результате почему результат будет, как показано ниже
{
"id": 2,
"name": "Message",
"slug": "Name",
"status": 1,
"fields": [
{
"id": 6,
"lead_section_id": 2,
"field_type": "text",
"help_text": "This is a tool tip",
"name": "Test11",
"placeholder": "Enter the name",
"slug": "test11111111",
"status": 9
},
{
"id": 7,
"lead_section_id": 2,
"field_type": "text",
"help_text": "This is a tool tip",
"name": "Test11",
"placeholder": "Enter the name",
"slug": "test11asdasd111111",
"status": 1
}
]
}
Запрос в golang, например
var queryIn []bson.M
queryIn = append(queryIn, bson.M{"_id": 2})
queryIn = append(queryIn, bson.M{"fields.status": bson.M{operator: []int{1,0}}})
// database connection
getCollection := sessionCopy.DB("Database").C("lead_section")
pipe := getCollection.Pipe([]bson.M{
bson.M{
"$lookup": bson.M{
"localField": "_id",
"from": "lead_field",
"foreignField": "lead_section_id",
"as": "fields"}},
bson.M{"$match": bson.M{"$and": queryIn}},
})
err = pipe.One(&data)
fmt.Println(data, err)
Как я могу исправить этот запрос при любых предложениях, пожалуйста.