Answer the question
In order to leave comments, you need to log in
How to change sorting by MongoDB document in Laravel?
Hello.
Tired of looking for information on changing sorting ...
In general, there are documents
{
"_id": ObjectId("1974a8d27745ffb9644da8e1"),
"sort": 0
}
{
"_id": ObjectId("2974a8d27745ffb9644da8e2"),
"sort": 1
}
{
"_id": ObjectId("3974a8d27745ffb9644da8e3"),
"sort": 2
}
$ids = [
"2974a8d27745ffb9644da8e2",
"3974a8d27745ffb9644da8e3",
"1974a8d27745ffb9644da8e1"
]
foreach ($ids as $id) {
$each[] = ['_id' => new \MongoDB\BSON\ObjectID($id)];
}
$result = Modules::update([
'sort' => [
'$each' => $each,
'$sort' => 1
]
]);
foreach ($ids as $sort=>$id) {
Modules::where([
'_id' => $id
])->update([
'sort' => $sort
]);
}
Answer the question
In order to leave comments, you need to log in
I love it when the complexity of the question is marked "Elementary", while not answering the question itself))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question