[英]find all the documents that is the latest by a field and distinct by another field in mongodb collection
[英]MongoDB find documents with a matching field in another collection
known = [{ system_id : 1234},
{ system_id : 1235},
{ system_id : 1236},
{ system_id : 1237}]
peeps = [
{system_id: 1234, name : bob},
{system_id: 1232, name : jim},
{system_id: 1231, name : dave},
{system_id: 1237, name : jeff}
]
如果我具有上述兩個集合,並且我想在peeps集合中找到在已知集合的文檔中存在system_ids的文檔,該怎么辦? 我目前正在跨Peeps運行聚合,在其中找到name和system_id的所有唯一組合,但是我需要排除已知集合中未包含的所有內容。
db.peeps.aggregate({
$lookup : {
from : "known",
localField : "system_id",
foreignField : "system_id",
as : "someField"
}
},{
$match : {
"someField.0" : {
$exists : true
}
}
},{
$project : {
"someField" : 0
}
})
這將輸出所有在knwon中具有匹配的system_id的窺視。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.