2016-06-11 113 views
7

這些是我的模式(主題是家長和包含的「思想的列表):在貓鼬填充嵌套數組 - Node.js的

var TopicSchema = new mongoose.Schema({ 
    title: { type: String, unique: true }, 
    category: String, 
    thoughts: [ThoughtSchema] 
}, { 
    timestamps: true, 
    toObject: {virtuals: true}, 
    toJSON: {virtuals: true} 
}); 

var ThoughtSchema = new mongoose.Schema({ 
    text: String, 
    author: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, 
    votes:[{ 
    _id:false, 
    voter: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, 
    up: Boolean, 
    date: {type: Date, default: Date.now} 
    }] 
}, { 
    timestamps: true, 
    toObject: {virtuals: true}, 
    toJSON: {virtuals: true} 
}); 

.... 

我想讀思想的作家和改變我的獲取主題API像這樣:

... 
    var cursor = Topic.find(query).populate({ 
    path: 'thoughts', 
    populate: { 
     path: 'author', 
     model: 'User' 
    } 
    }).sort({popularity : -1, date: -1}); 

    return cursor.exec() 
    .then(respondWithResult(res)) 
    .catch(handleError(res)); 
... 

但作者是空的。我也沒有在控制檯中得到任何錯誤。這裏有什麼問題?

編輯:其實我不需要思想作爲模式,它沒有自己的數據庫集合。它將被保存在主題中。但爲了使用時間戳選項,我需要將其內容提取到新的本地模式ThoughtSchema。但是我現在直接在主題的想法數組中定義了thinkSchema的內容,但它仍然無效。

Edit2:這是執行前的遊標對象。不幸的是,我不能在Webstorm調試,這是從節點督察截圖:使用Model.populate

enter image description here

+0

@Theodore答案對我來說看起來是正確的:查詢的內容是什麼?你有選擇嗎? – pasine

+0

我已經上傳了遊標對象內容的截圖,在它執行之前 – akcasoy

回答

0

如何

Topic.find(query).populate('thoughts') 
.sort({popularity : -1, date: -1}) 
.exec(function(err, docs) { 
    // Multiple population per level 
    if(err) return callback(err); 
    Topic.populate(docs, { 
    path: 'thoughts.author', 
    model: 'User' 
    }, 
    function(err, populatedDocs) { 
    if(err) return callback(err); 
    console.log(populatedDocs); 
    }); 
}); 
+0

nope :(....... – akcasoy

0

你試試?

Topic.find(query).populate('thoughts') 
.sort({popularity : -1, date: -1}) 
.exec(function(err, docs) { 
    // Multiple population per level 
    if(err) return callback(err); 
    Thought.populate(docs, { 
    path: 'thoughts.author', 
    model: 'User' 
    }, 
    function(err, populatedDocs) { 
    if(err) return callback(err); 
    console.log(populatedDocs); 
    }); 
}); 

UPDATE:
你可以deep populate這樣的嘗試:

Topic.find(query).populate({ 
    path: 'thoughts', 
    populate: { 
    path: 'author', 
    model: 'User' 
    } 
}) 
.sort({popularity : -1, date: -1}) 
.exec(function(err, docs) { 
    if(err) return callback(err); 
    console.log(docs); 
}); 
+0

'Thought'沒有被導出。我也不知道如何導出多個模式......這是唯一的出口行:出口默認mongoose.model('主題',TopicSchema); – akcasoy

+0

你不需要導出它,因爲你正在使用它作爲嵌入式模式。在這種情況下,我確認@ Theodore的答案應該工作。檢查你的數據庫?是否正確保存'作者'的引用? – pasine

+0

是的..作者字段有用戶的ID。但我不知道怎麼調試webstorm的服務器端代碼,看看如何查詢..我會嘗試在接下來的幾天中進行調試 但是關於「思想」模式..當我使用Thought.populate的時候,如你所建議的那樣,應用程序不能被編譯......像「Thought is undefined」那樣出現在終端 – akcasoy

0

這些都是模式:

var TopicSchema = new mongoose.Schema({ 
    title: { type: String, unique: true }, 
    category: String, 
    thoughts: [ThoughtSchema] 
}, { 
    timestamps: true, 
    toObject: {virtuals: true}, 
    toJSON: {virtuals: true} 
}); 

var ThoughtSchema = new mongoose.Schema({ 
    text: String, 
    author: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, 
    votes:[{ 
    _id:false, 
    voter: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, 
    up: Boolean, 
    date: {type: Date, default: Date.now} 
    }] 
}, { 
    timestamps: true, 
    toObject: {virtuals: true}, 
    toJSON: {virtuals: true} 
}); 

你嘗試聚集而是填充的。聚合使用$lookup可以更容易地填充嵌入的數據。嘗試下面的代碼。

UPDATE

Topic.aggregate([{$unwind: "$thoughts"},{ $lookup: {from: 'users', localField: 'thoughts.author', foreignField: '_id', as: 'thoughts.author'}},{$sort:{{popularity : -1, date: -1}}}],function(err,topics){ 
console.log(topics) // `topics` is a cursor. 
// Perform Other operations here. 
}) 

說明

$放鬆:解構陣列場從輸入文檔輸出爲每個元素的文檔。

$查找:$ lookup階段在輸入文檔的字段與「已加入」集合的文檔中的字段之間執行相等匹配。查找人口的工作。

$查找就像

:這個說從中收集數據需要被填充(users在這種情況下)。

localField:這是需要填充的本地字段。 (在這種情況下爲thoughts.author)。

foreignField:這是本從中數據需要被填充(_id字段中users收集在該場景)的集合中的外來場。

as:這是您想要顯示連接值的字段。 (這將把ideas.author的id作爲thoughts.author文檔)。

希望這個工程。

+0

「你有沒有嘗試聚合而不是填充」對不起..但聚合通常用於「匹配」,而不是「發現」據我所知..所以它不是一種替代方案填充,但'找到'我認爲..所以我不知道如何將此集成到我的代碼..聚合返回也是一個遊標對象,必須執行後?你能否在問題中更新我的第二個代碼並寫出所有的行? – akcasoy