db.getCollection('market').aggregate([
{
"$match":{
market_no: 137567
}
},
{
"$project": {
market_username: 1
}
},
{
"$lookup": {
"from": "profile",
"localField": "market_username",
"foreignField": "profile_username",
"as": "profile"
}
},
{
"$unwind": {
"path": "$profile",
"preserveNullAndEmptyArrays": true
}
},
{
"$unwind": {
"path": "$profile.board_auth",
"preserveNullAndEmptyArrays": true
}
},
{
"$project": {
"profile.board_auth":1
}
},
])
this is the result
/* 1 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"profile" : {
"board_auth" : {
"brd_no" : 110,
"auth_bit" : 7,
"brd_type" : "board"
}
}
}
/* 2 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"profile" : {
"board_auth" : {
"brd_type" : "club",
"auth_type" : "owner",
"brd_no" : 10103,
"auth_bit" : 7
}
}
}
/* 3 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"profile" : {
"board_auth" : {
"brd_type" : "academy",
"auth_type" : "owner",
"brd_no" : 30002,
"auth_bit" : 7
}
}
}
/* 4 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"profile" : {
"board_auth" : {
"brd_type" : "special",
"auth_type" : "member",
"brd_no" : 110,
"auth_bit" : 0,
"badge_no" : 4
}
}
}
I want to lookup depend the “profile.board_auth.brd_type”
switch profile.board_auth.brd_type {
case club: "club"
case academy: "academyInfo"
default: "badge"
}
so I tried to make mongodb Query
db.getCollection('market').aggregate([
{
"$match":{
market_no: 137567
}
},
{
"$project": {
market_username: 1
}
},
{
"$lookup": {
"from": "profile",
"localField": "market_username",
"foreignField": "profile_username",
"as": "profile"
}
},
{
"$unwind": {
"path": "$profile",
"preserveNullAndEmptyArrays": true
}
},
{
"$unwind": {
"path": "$profile.board_auth",
"preserveNullAndEmptyArrays": true
}
},
{
"$project": {
"profile.board_auth":1
}
},
{
"$lookup": {
"from": "club",
"let": {
"brd_type": "$profile.board_auth.brd_type",
"brd_no": "$profile.board_auth.brd_no"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [
{ "$eq": ["$$brd_type", "club"] },
{ "$eq": ["$$brd_no", "$club_no"]}
]
}
}
}
],
"as": "clubLookup"
}
},
{
"$lookup": {
"from": "academyInfo",
"let": {
"brd_type": "$profile.board_auth.brd_type",
"brd_no": "$profile.board_auth.brd_no"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [
{ "$eq": [ "$$brd_type", "academy" ] },
{ "$eq": [ "$$brd_no", "$academy_no" ] }
]
}
}
}
],
"as": "academyLookup"
}
},
{
"$lookup": {
"from": "badge",
"let": {
"brd_type": "$profile.board_auth.brd_type",
"badge_no": "$profile.board_auth.badge_no"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [
{ "$eq": [ "$$brd_type", "special" ] },
{ "$eq": [ "$$badge_no", "$badge_no" ] }
]
}
}
}
],
"as": "badgeLookup"
}
},
{
"$addFields": {
"allLookup": {
"$setUnion": ["$clubLookup","$academyLookup","$badgeLookup"]
}
}
},
{
"$unwind": {
"path": "$allLookup",
"preserveNullAndEmptyArrays": true
}
},
{
"$addFields": {
"allLookup": {
"$cond": [
{
"$not": "$allLookup"
},
"$$REMOVE",
{
"$mergeObjects": [
[
{
"brd_type": "$profile.board_auth.brd_type",
"auth_type": "$profile.board_auth.auth_type",
"club_url_name": "$allLookup.club_url_name",
"club_emblem_image": "$allLookup.club_emblem_image",
"academy_url_name": "$allLookup.academy_url_name",
"academy_emblem_image": "$allLookup.academy_emblem_image",
"badge_name": "$allLookup.badge_name",
"badge_image": "$allLookup.badge_image",
"_id": "$allLookup._id"
}
]
]
}
]
}
}
},
{
"$project": {
"allLookup":1
}
},
])
i got final result
/* 1 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2")
}
/* 2 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"allLookup" : {
"brd_type" : "club",
"auth_type" : "owner",
"club_url_name" : "10103",
"club_emblem_image" : [ "image" ],
"_id" : ObjectId("64e4528711945660dbcabbc7")
}
}
/* 3 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"allLookup" : {
"brd_type" : "academy",
"auth_type" : "owner",
"academy_url_name" : "30002",
"academy_emblem_image" : [ "image" ],,
"_id" : ObjectId("6618bf61cf9e95efa204fa61")
}
}
/* 4 */
{
"_id" : ObjectId("6614b6f0185079cb3d8a22f2"),
"allLookup" : {
"brd_type" : "special",
"auth_type" : "member",
"badge_name" : "name",
"badge_image" : [ "/admin/badge/re4mo_level_3.png"],
"_id" : ObjectId("63eb53fde7df8cb008383519")
}
}
but my mongodb query tried lookup 3 times,
i want to lookup once depend the profile.board_auth.brd_type
so get the result faster and simple.
thank you for helping me.