Я использую $lookup
с агрегацией $match
, чтобы получить результат из двух коллекций на основе определенного условия.Ниже приведен мой запрос, который я использую в коде golang: -
getCollection := sessionCopy.DB("Database").C("lead_section")
pipe := getCollection.Pipe([]bson.M{
bson.M{"$match": bson.M{"status": 1}},
bson.M{
"$lookup": bson.M{
"localField": "_id",
"from": "lead_field",
"foreignField": "lead_section_id",
"as": "custom_fields"}},
// bson.M{"$unwind": "$custom_fields.status"},
bson.M{"$match": bson.M{"custom_fields.status": 1}}})
запрос, приведенный выше, вернет мне этот результат
[
{
"id": 1,
"name": "Message",
"status": 1,
"custom_fields": [
{
"id": 3,
"lead_section_id": 1,
"field_type": "text",
"help_text": "This is a tool tip",
"name": "Test11",
"status": 0
},
{
"id": 4,
"lead_section_id": 1,
"field_type": "text",
"help_text": "This is a tool tip",
"name": "Test11",
"status": 1
},
{
"id": 5,
"lead_section_id": 1,
"field_type": "text",
"help_text": "This is a tool tip",
"name": "Test11",
"status": 1
}
]
}
]
, но я хочу, чтобы результат, полученный с помощью канала, был столько status 1
.Но в custom_fields
первая запись с id:3
, имеющая status:0