Ruby on rails Mongodb:组合两个集合以基于其他集合中的键的值获取数据-(mongoid-rails4)
我试图通过组合bolg_post和用户文档来获得结果集Ruby on rails Mongodb:组合两个集合以基于其他集合中的键的值获取数据-(mongoid-rails4),ruby-on-rails,mongodb,mapreduce,mongoid,Ruby On Rails,Mongodb,Mapreduce,Mongoid,我试图通过组合bolg_post和用户文档来获得结果集 blog_post # embeds many comments doc { "_id" : ObjectId( "55ea003e4102135d9d000677" ), "text" : "This is a post about natural beauties of hawaii.. blah blah..", "author_id": "345345026743545647567567", # references
blog_post # embeds many comments doc
{
"_id" : ObjectId( "55ea003e4102135d9d000677" ),
"text" : "This is a post about natural beauties of hawaii.. blah blah..",
"author_id": "345345026743545647567567", # references to User doc id
"updated_at" : Date( 1441398846696 ),
"created_at" : Date( 1441398846696 ),
"comments" : [
{ "_id" : ObjectId( "55ea003e4102135d9d000002" ),
"text" : "Very good article",
"user_id": "83753iy45735y84934759",
"updated_at" : Date( 1441400501867 ),
"created_at" : Date( 1441400501867 )
},
{ "_id" : ObjectId( "345a003e41026743545645454" ),
"text" : "Hawaii is my fav",
"user_id" : "55e7668c4102132ea8000066", # references to User doc id
"updated_at" : Date( 1441400501867 ),
"created_at" : Date( 1441400501867 )
}
]
}
User # has many blog_posts
{
"_id" : ObjectId( "83753iy45735y84934759" ),
"name" : "John abc",
"status": "active", # could be inactive, pending, deleted etc.
"age": 29,
"updated_at" : Date( 1441400501867 ),
"created_at" : Date( 1441400501867 )
}
{
"_id" : ObjectId( "55e7668c4102132ea8000066" ),
"name" : "Lisa xyz",
"status": "inactive", # could be inactive, pending, deleted etc.
"age": 24,
"updated_at" : Date( 1441400501867 ),
"created_at" : Date( 1441400501867 )
}
{
"_id" : ObjectId( "345345026743545647567567" ),
"name" : "David uyi",
"status": "active", # could be inactive, pending, deleted etc.
"age": 39,
"updated_at" : Date( 1441400501867 ),
"created_at" : Date( 1441400501867 )
}
我只想获得那些博客文章,其中作者状态在用户文档中处于活动状态。
同样的,我只想得到一篇文章的评论,这些评论是由状态在用户文档中处于活动状态的用户发布的
{
"blog_id" : "55ea003e4102135d9d000677",
"text" : "This is a post about natural beauties of hawaii.. blah blah..",
"author": {
"author_id": "345345026743545647567567",
"name" : "David uyi",
"age": 39
}
"updated_at" : Date( 1441398846696 ),
"created_at" : Date( 1441398846696 ),
"comments" : [
{ "comment_id" : ObjectId( "55ea003e4102135d9d000002" ),
"text" : "Very good article",
"user" : {
"user_id":
"name" : "John abc",
"age": 29
}
}
# here Lisa's comment is not shown as she is inactive
]
}
但关键是我需要分页,这意味着,假设我只需要5条记录,每个查询结果限制为0,5英寸。
所以最简单的方法就是抓取前5篇博客文章文档。然后从他们那里获取用户id,然后转到用户文档并
然后查找这些用户是否处于活动状态。如果没有,则删除这些条目。但这样的话,我可能会吃不到
5结果集。那我怎么能一次完成呢。假设我有100,00,00个用户和100000个帖子。所以这将是一个糟糕的设计
如果我按照前面提到的方式,而不是一次完成我想你可以结合mongoid的“跳过”和“限制”方法,对给定用户的所有博客进行分页查询,跳过n*5个结果,限制为5个results@jmuet我希望它出现在一个查询中,而不是编写两个单独的查询Safaik Mongo不允许多个集合的联接;您可以查询单个集合。或者,如果“状态”字段不经常更改,那么您可以将其包括在博客文章中以及作者id中,并在用户更改状态时在所有文档中进行更新。