I have some documents in a MongoDB collection with this schema:
{
"_id": {
"$oid": "60c1e8e318afd80016ce58b1"
},
"searchPriority": 1,
"isLive": false,
"vehicleCondition": "USED",
"vehicleDetails": {
"city": "Delhi"
}
},
{
"_id": {
"$oid": "60c1f2f418afd80016ce58b5"
},
"searchPriority": 2,
"isLive": false,
"vehicleCondition": "USED",
"vehicleDetails": {
"city": "Delhi"
}
},
{
"_id": {
"$oid": "60cb429eadd33c00139d2be7"
},
"searchPriority": 1,
"isLive": false,
"vehicleCondition": "USED",
"vehicleDetails": {
"city": "Gurugram"
}
},
{
"_id": {
"$oid": "60c21be618afd80016ce5905"
},
"searchPriority": 2,
"isLive": false,
"vehicleCondition": "USED",
"vehicleDetails": {
"city": "New Delhi"
}
},
{
"_id": {
"$oid": "60e306d29e452d00134b978f"
},
"searchPriority": 3,
"isLive": false,
"vehicleCondition": "USED",
"vehicleDetails": {
"city": "New Delhi"
}
}
vehicleCondition can be NEW or USED, isLive can be true or false and searchPriority will be an integer between 1 to 3. (lower number means it should be higher in search result)
Here, except _id none of the other fields are unique. I have created a compound index on isLive, vehicleDetails.city and searchPriority.
In my application I will perform some queries of this form:
- find all cars where
isLiveistrue,vehicleDetails.cityis eitherDelhiorNew DelhiorGurugramandvehicleConditionisUSED(orNEW).
For this, I can do a find query like this:
db.collection.find({"isLive": true, "vehicleDetails.city": { $in: [ "Gurugram", "Delhi", "New Delhi" ] }, "vehicleCondition": "USED" }, {})
I want the results of this query sorted in this order:
- All cars belonging to the 1st city inside
$inarrray in the find query, having lowest priority - All cars belonging to the 1st city inside
$inarrray in the find query, having 2nd lowest priority - All cars belonging to the 1st city inside
$inarrray in the find query, having 3rd lowest priority - All cars belonging to the 2nd city inside
$inarrray in the find query, having lowest priority - All cars belonging to the 2nd city inside
$inarrray in the find query, having 2nd lowest priority - All cars belonging to the 2nd city inside
$inarrray in the find query, having 3rd lowest priority All cars belonging to the 3rd city inside$inarrray in the find query, having lowest priority - All cars belonging to the 3rd city inside
$inarrray in the find query, having 2nd lowest priority - All cars belonging to the 3rd city inside
$inarrray in the find query, having 3rd lowest priority
How can I do this? Since the number of documents returned by this query could be very large, I will be using pagination to limit the number of returned documents. Will this extra requirement have any effect on the possible solution for this problem?