例如,如果我有以下架構(爲了簡潔起見,大大簡化了)。我如何通過標籤搜索帖子?我知道如果標籤文檔集合被嵌入,但是我想讓Tag保留在他們自己的集合中,該怎麼做。搜索Mongoose中的嵌入對象
PostSchema = new Schema({
title: String
body: String
tags: [{type: Schema.ObjectId, ref: 'Tag' }]
});
TagSchema = new Schema({
name: String
});
// Here is what I've tried
Post.find({'tags.name':'javascript'})
.populate('tags') // Is it necessary to join the collections?
.run(function(err, posts) {
console.log('posts: ', posts);
});
不應該是'Post.find({'tags.name':'javascript'})'在你的代碼? –
是裏卡多。我試圖修剪掉那些無關緊要的東西,把錯誤的查詢放在那裏。感謝您致電 –