S
S
source20032016-12-11 23:22:25
MongoDB
source2003, 2016-12-11 23:22:25

How to search through a collection?

In the collection there are documents of the form {_id: id, "custom-field": true} or just {_id: id}
Do I correctly select all values ​​where custom-field = true, or it does not exist at all.
db.collection.find({"custom-field": {$in: [true]}}) - When you need to select documents where there is custom-field = true
db.collection.find({"custom-field": { $nin: [true]}}) - All other documents where custom-field is absent or false
One more question: I heard that skip in mongodb is not very fast, is it? I will need to iterate over a collection with 20 million documents, I think to select them by 1000 and skip or is this a bad idea? Or if I select 20KK documents at once and put them into an array, then if there is RAM, everything will work well? Or is it not worth it at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-12-12
@lega

db.collection.find({"custom-field": {$in: [true]}})

db.collection.find({"custom-field": true})
db.collection.find({"custom-field": {$ne: true}})
It does not work very fast everywhere, the specifics of tree indexes. Therefore, it is better to use a sorting field for pagination, then it will be fast, for example {_id: {$gt: lastReceivedID}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question