My data model is:
class User
include MongoMapper::Document
many :posts
end
class Post
include MongoMapper::EmbeddedDocument
end
Is there any way to fetch all Posts of all Users?
P.S. User.all.map(&:posts).flatten sounds really slow.
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
I completely understand the tradeoffs of embedding posts into User.
However, it's more an example than a real-world architecture.
The question is how to find an embedded document by specific criterion
or criteria, in my case, find a post by id. As far as I can see in
embedded_document.rb there's no #first method defined. So, the answer
might be going into mongo-ruby-driver internals and doing something
like User.collection.find({ "posts.id" =>
"4b9130ea37a08e6477000002" }), however, it returns Cursor object with
#count == 0. Am I missing something?
On Mar 6, 9:34 pm, John Nunemaker <nunema...@gmail.com> wrote:
> I would not embed posts in user. Posts will grow over time and most likely
> need to be larger than 4mb doc limit in mongo. Make Post a document and add
> key :user_id, ObjectId to Post model.
>
> On Sat, Mar 6, 2010 at 8:57 AM, graf.otodrakula
> <graf.otodrak...@gmail.com>wrote:
The problem there could be that the string "4b9130ea37a08e6477000002"
isn't really the id. The id is a Mongo::ObjectId. So
User.collection.find_one({ "posts.id" =>
Mongo::ObjectId.from_string("4b9130ea37a08e6477000002") }) might work.
--
Have Fun,
Steve Eley (sfe...@gmail.com)
ESCAPE POD - The Science Fiction Podcast Magazine
http://www.escapepod.org
User.collection.mapreduce(map_func, reduce_func)
where the functions are:
map_func = <<-JS
function() {
this.posts.forEach( function(post) {
emit(post._id, post);
})
}
JS
reduce_func = <<-JS
function(k,v) {
return v[0];
}
JS
Buy your high quality real or fake passport,(fani...@gmail.com) Counterfeit Bills,Real and Fake Driver’s licenses, ID cards, visas, stamps, diploma, certificates, degrees, citizenship and other products for a number of countries like: USA, Australia, Belgium, Brazil, Canada, Italy, Finland, France, Germany, Israel, Russia,Mexico, Finland,Netherlands ,South Africa,Spain,United Kingdom.Japan when producing; magnetic encoded strips and/or scan able bar-code. UV-spectrum analysis test standards,magnetic strip,
Watch video here for more details........... http://vimeo.com/82973635
Contact us............... fani...@gmail.com
Email.......................... fani...@yahoo.com
SKYPE US for quick chat …………….. fandena.fandena
SKYPE US for quick chat …………….. fandena.fandena
SKYPE US for quick chat …………….. fandena.fandena
Contact e-mails: fani...@gmail.com Technical support: fani...@gmail.com