2017-04-04 55 views
1

這是預訂表

booking [ 
    { 
     "_id" : "0J0DR", 
     "user" : "MN90L", 
     "property" : "58669471869659d70b424ea7", 
     "checkin" : 1488758400, 
     "checkout" : 1489363200 
    }, 
    { 
     "_id" : "0PDLR", 
     "user" : "7CSEF", 
     "property" : "586694ea869659d70b424eb3", 
     "checkin" : 1488326400, 
     "checkout" : 1498780800 
    } 
] 

這是用戶表

users [ 
    { 
     "_id" : "4M4KE", 
     "email" : "[email protected]", 
     "name" : "Torben" 
    }, 
    { 
     "_id" : "MN90L", 
     "email" : "[email protected]", 
     "name" : "Mr. Booker" 
    }, 
    { 
     "_id" : "GF37A", 
     "email" : "[email protected]", 
     "name" : "Whatever" 
    }, 
    { 
     "_id" : "7CSEF", 
     "email" : "[email protected]", 
     "name" : "Miss. Booker" 
    }, 
    { 
     "_id" : "W0LG9", 
     "email" : "[email protected]", 
     "name" : "Whatever" 
    } 
] 

這是我的查詢,除了每次訂房記錄偉大的工程,得到所有的用戶連接而不只是users._id = booking.user

db.getCollection('booking').aggregate([{ 
    $match: { 
     checkin : {$lte: (1512145439)}, 
     checkout: {$gte: (1483203600)}, 
    } 
}, { 
    $lookup: { 
     from: "users", 
     localField: "users._id", 
     foreignField: "booking.user", 
     as: "users" 
    } 
}, { 
    $unwind: "$users" 
}]) 

我在做什麼錯?

我嘗試過和沒有$ unwind。

我需要做一些$匹配或者是localField foreignField嗎?

我db.version()= 3.2.12

+0

其實與$放鬆它創建10條記錄中的結果爲每個用戶= 2個訂票×5級的用戶一個預訂記錄= - 不會放鬆$它創建的每個預訂記錄內的所有用戶。 – torbenrudgaard

+0

你有'localField'和'foreignField'混合在一起。嘗試'localField:「用戶」, foreignField:「_id」,' – Veeram

+0

@Veeram我嘗試了兩個,但得到完全相同的結果。 10條記錄(2個預訂×5個用戶)。這真的很奇怪。 – torbenrudgaard

回答