【发布时间】:2012-01-13 11:48:20
【问题描述】:
例如,如果我有以下架构(为简洁起见,非常简化)。如何按标签搜索帖子?如果嵌入了标签文档集合,我知道该怎么做,但我想将标签保留在他们自己的集合中。
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'})吗? -
是的,里卡多。我试图去掉不相关的东西,把错误的查询放在那里。感谢收看