Answer the question
In order to leave comments, you need to log in
Editing and deleting nested array elements in MongoDB on Go?
Hello!
People tell me how to implement operations (editing \ deleting) with elements of an array nested in a document?
Document structure:
{
"user_id": 123
"categories": [
{
"category_name": "entertainment"
"tags": [
{
"list_name": "games",
"discription": "video games tags",
"list": ["psp","xbox","pc"]
},
{
"list_name": "tvshow",
"discription": "any tvshow",
"list": ["got","dexter","arrow"]
}
]
},
{
"category_name": "sports"
"tags": [
{
"list_name": "summer",
"discription": "summer sport",
"list": ["tennis","swimming","jumping"]
},
{
"list_name": "winter",
"discription": "winter sport",
"list": ["hockey","biathlon","ski"]
}
]
}
]
}
colQuerier := bson.M{
"user_id": "123",
"categories": bson.M{
"$elemMatch": bson.M{
"category_name": "entertainment",
"tags": bson.M{
"$elemMatch": bson.M {"list_name": "tvshow"}
}
}
}
}
change := bson.M{"$pull": bson.M{"categories.$.tags": bson.M{"list_name": "tvshow"}}}
err = c.Update(colQuerier, change)
Answer the question
In order to leave comments, you need to log in
Issue resolved!
Perhaps through a crutch (because it could probably be implemented through $map or $filter), but in the end everything works as it should.
//выбираем документ и устанавливаем на него курсор.
colQuerier := bson.M{"user_id": "123", "categories": bson.M{"$elemMatch": bson.M{"category_name": "entertainment", "tags": bson.M{"$elemMatch": bson.M{"list_name": "tvshow"}}}}}
//удалаем элемент вложенного массива использя оператор проекции
change := bson.M{"$pull": bson.M{"categories.$.tags": bson.M{"list_name": "tvshow"}}}
err = c.Update(colQuerier, change)
//создаем пустой интерфейс для получения данных агрегации
var resp interface{}
//Используем механизм агрегации данных с проекцией для получения индекса основного массива
pipe := c.Pipe([]bson.M{{"$match": bson.M{"user_id": user_id}}, {"$project": bson.M{"_id": 0, "matchedIndex": bson.M{"$indexOfArray": []string{"$categories.category_name", "entertainment"}}}}}).One(&resp)
//проекция возвращает такую структуру map[matchedIndex:0], где 0 соответсвующий индекс элемента
//Создаем тип для проекции
var index map[string]int
//Преобразуем пустой интерфейс в тип данных предоставляемый нашей проекцией
byteData, _ := json.Marshal(resp)
err = json.Unmarshal(byteData, &index)
//Преобразовываем индекс в строку
i := strconv.Itoa(int(index["matchedIndex"]))
//Выбираем документ и устанавливаем на него курсор.
colQuerier := bson.M{"user_id": user_id, "categories." + i + ".tags": bson.M{"$elemMatch": bson.M{"list_name": "tvshow"}}}
change := bson.M{"$set": bson.M{ "categories." + i + ".tags.$.list_name": "serials"}}
//Изменяем данные
err = c.Update(colQuerier, change)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question