D
D
Dream2021-05-22 17:15:18
Python
Dream, 2021-05-22 17:15:18

How to find the position of an item in a PyMongo list?

Greetings, how to find the position of an item and refer to it ($pull) in a list (Array)?

Example:

list1 = collection_name.find_one({"_id": ...})["Список"]
collection_name.update_one({"_id": ...}, {"$pull": {"list": ...}})

60a90fb0abdf5406733843.png
On the screen on the left there are positions of objects, so how to access them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Romanov, 2021-05-24
@dreameddd

If this is an array of strings, then you need to specify the desired value

collection_name.update_one({"_id": ...}, {"$pull": {"list": "Позиция 0"}})

Will remove the element "Position 0" from the list list
If you need to change a specific element then:
collection_name.update_one({_id: ..., "list": "Позиция 0"}, {"$set: {"list.$": "Позиция 00"}})

First, it will find what position in the array the value "Position 0" has, then it will change it to "Position 00"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question