Answer the question
In order to leave comments, you need to log in
How to add counter to MongoDB pipeline?
Faced with the task of updating the "order" field in many rows in mongoDB, everything seems to be simple, but I need to insert a number in the "order" field with an iterator step. But I didn’t find anywhere how to set a mutable variable in the pipeline in order to build a function based on it.
There is a set of fields like this:
[
{"_id": "619ff3d26812666d818a5cd6", "sheet": 46, "order": 32, "name": "Row 1"},
{"_id": "619ff3d26812666d818a5cd7", "sheet": 46, "order": 1, "name": "Row 2"},
{"_id": "619ff3d26812666d818a5cd8", "sheet": 46, "order": 23, "name": "Row 3"},
]
db.collection.update({"sheet": 46}, {
$sort: {
"name": 1
},
$set: {
"order" : (вот тут мне нужно вставить итератор, который будет увеличиваться на +1 с каждой новой строкой)
}
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question