D
D
Dmitry Prilepsky2021-06-07 18:16:55
Python
Dmitry Prilepsky, 2021-06-07 18:16:55

How to order documents in mongodb by field?

There are two bases. In each entry like

[{'name':'Andrey Doncov', 'inner_id': 'and_d'}, {'name':'Denis Popov', 'inner_id': 'den'}]
I need to merge them into one and sort by the id field. (This field needs to be added) I wrote the following code
n_id =0
for cv in cvs:
    cv['id'] = n_id
    main_collection.update_one({'inner_id': cv['inner_id']}, {'$set': cv}, upsert=True)
    n_id += 1

After that, when I request the merged database, the id-shniks in it go out of order, i.e. first there is a record with id:189, then id:65, etc. How to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question