创build查询(join)或更正devise?

我有这个devisebuild立一些简单的查询有问题。 问题很简单,我想获得包含用户信息,这应该是在一个数组中的所有post,所以我可以传递给快递视图。

简单,对我来说,似乎错误的解决办法是find所有的posts(db.post.find({email:'mo@gmail.com'}) ,然后循环通过post,使一个查询查询用户集合和然后合并结果。

其他解决scheme将使用链接作者DBref这可能会更好,但我还没有发现如何进行查询。

 // User, the profile is not complete there will be more fields var u = { name: 'Michael', // Is not unique email: 'mo@gmail.com', // Should be also unique fbid: '4545454asdadsa' } db.user.insert(u); // User can have 0 to many posts var p = { title: 'Suprise', body: 'Well done', tags: ['first', 'post', 'english'], author: 'mo@gmail.com', created: new Date(), modified: new Date() }; db.post.insert(p); var p = { title: 'Weather in Estonia', body: 'Always looks bad', tags: ['bad', 'weather', 'estonia'], author: 'mo@gmail.com', created: new Date(), modified: new Date() } db.post.insert(p); 

 var p = { title: 'Suprise', body: 'Well done', tags: ['first', 'post', 'english'], author: { name: 'Michael', // Is not unique email: 'mo@gmail.com', // Should be also unique fbid: '4545454asdadsa' }, created: new Date(), modified: new Date() }; 

你embedded文件,你denormalize。 这是如何工作的。 现在,您可以获取所有post,而不必查询用户,因为他们已经在那里了。

数据结构

我会说明如何做到这一点。 我完全放弃了反规范化。 这意味着你永远不需要连接。 请注意,如果这只是一个不必要的东西,其中一些可以被删除。

post集合

 { title: String, body: String, tags: [String], author: { name: String, email: String, fbid: String }, created: Date, modified: Date, comments: [{ body: String, created: Date, modified: Date }] } 

用户集合

 { name: String, email: String, fbid: String, posts: [{ title: String, body: String, tags: [String], created: Date, modified: Date, comments: [{ body: String, created: Date, modified: Date }] }] } 

评论集合

 { body: String, created: Date, modified: Date, author: { name: String, email: String, fbid: String }, post: { title: String, body: String, tags: [String], created: Date, modified: Date comments: [{ body: String, created: Date, modified: Date }] } }