admin管理员组文章数量:1410712
I'm building a ride-sharing platform where rides have multiple stops stored as an array of objects in MongoDB. Each stop contains a cityName
field (e.g., "Lahore Pakistan"
).
Users search for rides by providing pickup
and drop
city names, but the issue is that city names in the database may have variations (e.g., "Lahore Pakistan"
instead of "Lahore"
).
I need a MongoDB aggregation query that:
- Matches rides where both pickup and drop exist in the stops array
- Uses regex for flexible city name matching
- Ensures case-insensitive search
- Returns ride details along with driver and vehicle info
Example Stops Data:
{
"stops": [
{ "cityName": "Lahore Pakistan" },
{ "cityName": "Multan Pakistan" },
{ "cityName": "Karachi Pakistan" }
]
}
If a user searches for:
pickup=Mardan&drop=Lahore
, it should return the ride if both cities exist in the ride's stops.
My Attempted Query (Not Working as Expected):
const pickupRegex = new RegExp(`^${pickup}`, "i");
const dropRegex = new RegExp(`^${drop}`, "i");
const rides = await Ride.aggregate([
{
$match: {
stops: {
$elemMatch: { cityName: pickupRegex }, // Match pickup city
$elemMatch: { cityName: dropRegex } // Match drop city
}
}
}
]);
The query does not return results as expected. I've tried multiple approaches, but it seems $elemMatch
does not work for matching two different elements in the same array.
How can I correctly filter rides where both pickup and drop locations exist in the stops
array using MongoDB aggregation with regex?
I'm building a ride-sharing platform where rides have multiple stops stored as an array of objects in MongoDB. Each stop contains a cityName
field (e.g., "Lahore Pakistan"
).
Users search for rides by providing pickup
and drop
city names, but the issue is that city names in the database may have variations (e.g., "Lahore Pakistan"
instead of "Lahore"
).
I need a MongoDB aggregation query that:
- Matches rides where both pickup and drop exist in the stops array
- Uses regex for flexible city name matching
- Ensures case-insensitive search
- Returns ride details along with driver and vehicle info
Example Stops Data:
{
"stops": [
{ "cityName": "Lahore Pakistan" },
{ "cityName": "Multan Pakistan" },
{ "cityName": "Karachi Pakistan" }
]
}
If a user searches for:
pickup=Mardan&drop=Lahore
, it should return the ride if both cities exist in the ride's stops.
My Attempted Query (Not Working as Expected):
const pickupRegex = new RegExp(`^${pickup}`, "i");
const dropRegex = new RegExp(`^${drop}`, "i");
const rides = await Ride.aggregate([
{
$match: {
stops: {
$elemMatch: { cityName: pickupRegex }, // Match pickup city
$elemMatch: { cityName: dropRegex } // Match drop city
}
}
}
]);
The query does not return results as expected. I've tried multiple approaches, but it seems $elemMatch
does not work for matching two different elements in the same array.
How can I correctly filter rides where both pickup and drop locations exist in the stops
array using MongoDB aggregation with regex?
1 Answer
Reset to default 11. Your stops
object is clearly repeating $elemMatch
as a key/property, which is either not allowed or one will override the other.
2. You don't need to use $elemMatch
here because you're only checking the value of one property, and not multiple properties of the same object. And you're expecting different cities to match the pickup and drop so it will be different elements in the array.
3. Use $match with $and
for the two conditions:
Ride.aggregate([
{
$match: {
$and: [
{ "stops.cityName": pickupRegex }, // Match pickup city
{ "stops.cityName": dropRegex } // Match drop city
]
}
}
])
Mongo Playground
本文标签: mongodbSearch Rides by City Name in Stops Array Using RegexStack Overflow
版权声明:本文标题:mongodb - Search Rides by City Name in Stops Array Using Regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744841094a2627927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论