各位好
我目前在用MongoDB作資料庫開發系統
有一個語法我百思不得其解不知道該如何作出來
我有一個buyer的collection
裡面有5個buyer資料,並且其聯絡資訊有建一個contact的array
每個buyer的聯絡資料有多也有少(不一定齊全)
我要作的是,撈出這些buyer中,contact資料有任意一組相同的,就要撈出來
以下是我建的collection資料buyer:
{
"buyer_name" : "Aron",
"contact" : [
{
"key" : "cellphone",
"value" : "0937888444"
},
{
"key" : "officephone",
"value" : "02-25867777"
},
{
"key" : "private_email",
"value" : "test1@gmail.com"
}
]
},
{
"buyer_name" : "Breakgod",
"contact" : [
{
"key" : "cellphone",
"value" : "0937888444"
},
{
"key" : "officephone",
"value" : "02-258666666"
},
{
"key" : "public_email",
"value" : "test21@gmail.com"
},
{
"key" : "private_email",
"value" : "test22@gmail.com"
}
]
},
{
"buyer_name" : "Cindy",
"contact" : [
{
"key" : "cellphone",
"value" : "0937888333"
},
{
"key" : "officephone",
"value" : "02-25862222"
},
{
"key" : "private_email",
"value" : "test3@gmail.com"
}
]
},
{
"buyer_name" : "Dragon",
"contact" : [
{
"key" : "cellphone",
"value" : "0937888222"
},
{
"key" : "officephone",
"value" : "02-25862222"
},
{
"key" : "private_email",
"value" : "test4@gmail.com"
}
]
},
{
"buyer_name" : "Even",
"contact" : [
{
"key" : "cellphone",
"value" : "0937888111"
},
{
"key" : "private_email",
"value" : "test5@gmail.com"
}
]
}
以下是我下的查詢語法:
db.buyer.aggregate([
{ $lookup:
{
from: "buyer",
let: { id: "$_id", name: "$buyer_name", kk: "$contact.key", vv: "$contact.value"},
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$contact.key", "$$kk"] },
{ $eq: [ "$contact.value", "$$vv"] },
{ $ne: ["$$id", "$_id" ] }
]
}
}
}
],
as: "comment"
}
}
])
根據我的查詢語法,就會查不出半筆符合的資料
我這語法會將所有contact的資料作key與value的比對
也就因此無法撈出我要的資料了
還請各位高手指點一下
如何可以作到更細部的查詢,使之可以查詢到contact的任意一筆相同的資料就可以符合條件撈出來,謝謝