我有國家表
{
"_id" : ObjectId("627cd43f48aea72fdc0d88e0"),
"county" : "india"
},
{
"_id" : ObjectId("627cd43f48aea72fdc0d88e1"),
"county" : "china"
}
和城市表
{
"_id" : ObjectId("627cd43f48aea72fdc0d88e0"),
"county_name" : "India"
},
{
"_id" : ObjectId("627cd43f48aea72fdc0d88e1"),
"county_name" : "China"
}
在國家表中,國家名稱為小寫字母,在城市表中,國家名稱為大寫字母。那么現在如何使用類似的條件來加入具有相同名稱的兩個集合。前任。像印度一樣獲取印度的資料
db.getCollection('countryTable').aggregate([
{
$lookup: {
from: "cityTable",
let: { county: "$county" },
pipeline: [{
$match: {
"$expr": {
"$regexMatch": {
"input": "$county_name",
"regex": "$$county",
"options": "i"
}
}
}
}],
as: "citydetails"
}
},
{ $unwind: "$citydetails" }
])
uj5u.com熱心網友回復:
使用 MongoDB v4.0,您可以$match使用$toLower
db.countryTable.aggregate([
{
$lookup: {
from: "cityTable",
let: {
county: "$county"
},
pipeline: [
{
$match: {
"$expr": {
$eq: [
{
"$toLower": "$$county"
},
{
"$toLower": "$county_name"
}
]
}
}
}
],
as: "citydetails"
}
},
{
$unwind: "$citydetails"
}
])
這是Mongo游樂場供您參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474015.html
下一篇:MongoDB從結果集中獲取交集
