K
K
kuzubina2020-05-18 11:34:56
MongoDB
kuzubina, 2020-05-18 11:34:56

How to reorder documents in mongoDB?

Hello everyone, tell me pliz, how can I change the order of documents in mongoDB?
The server returns an array of this kind

[
{
"_id": "5eb003b825dddd2fc438fd0a",
"name": "Пицца Милано",
"category": "Пицца",
"price": 129,
"url": "https://www.pizzaking.ua/resizer/resize/upload/catalog/e/e7c6ce8e7bffbd7fc86a4c119262da24-472-472-c.jpg",
"description": "sfsfsdfssdsdfsd",
"__v": 0
},
{
"_id": "5eb004b03950840688b1834f",
"name": "Black Бургер",
"category": "Бургеры",
"price": 199,
"url": "https://ambar.net.ua/image/data/2398.jpg",
"description": "Самый сочный бургер",
"__v": 0
},
{
"_id": "5eb008c3f65282196c1a6c51",
"name": "Терамису",
"category": "Десерты",
"price": 55,
"url": "https://v.img.com.ua/b/orig/d/e8/12db2094c5da2d0c884f862c62c30e8d.jpg",
"description": "Нежное пироженое",
"__v": 0
}
]

on vue.js I display the whole thing and next to each position there are buttons with the ability to raise it higher or lower it. How to implement so that, for example, when you click on raise the second element, it becomes the first and the first the second, and save it in the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Didenko, 2020-05-18
@kuzubina

Add an order field to the schema model and update this field. On the back, implement a default order based on this parameter, for example. Perhaps mongo has its own default sorter, you need to google it, but there must be a field based on which you need to sort

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question