Reputation: 184
Given a collection of rows like
------------------------
date | time | value
------------------------
20190101| 0930 | 0.00
20190101| 0931 | 1.23
20190102| 0930 | 9.83
20190103| 0745 | NaN
....
------------------------
an I created a compound index (e.g. (date, time)) on the collection. What is the fastest way to query for records whose (date, time) are in the list -- for example,
query items that match:
{date:20190101, time:0931} or {date:20190102, time:0930}
should return
------------------------
date | time | value
------------------------
20190101| 0931 | 1.23
20190102| 0930 | 9.83
------------------------
There is a $in
aggregation but idk how to use it on multiple fields. By the way I am using pymongo.
Upvotes: 0
Views: 51
Reputation: 116
Let's get an example of a query using $in:
db.collection.find({
date: {
$in: [date0, date1, date2]
}
})
This same operation has the same result as a sequence of $or :
db.collection.find({
$or: [{
date: date0
},
{
date: date1
},
{
date: date2
}]
})
With this in mind, is possible to have the same behavior as an $in statement with multiple fields by translating it to this $or notation:
db.collection.find({
$or: [{
date: date0,
timestamp: time0
},
{
date: date1,
timestamp: time1
},
{
date: date2,
timestamp: time2
}]
})
Upvotes: 1