Finding all embedded documents

140 views
Skip to first unread message

graf.otodrakula

unread,
Mar 6, 2010, 8:57:11 AM3/6/10
to MongoMapper
Hello guys and gals.

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.

John Nunemaker

unread,
Mar 6, 2010, 2:34:38 PM3/6/10
to mongo...@googlegroups.com
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.


--
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

graf.otodrakula

unread,
Mar 8, 2010, 3:28:20 AM3/8/10
to MongoMapper
Thank you for your fast reply.

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:

John Nunemaker

unread,
Mar 9, 2010, 9:23:41 AM3/9/10
to mongo...@googlegroups.com
There is a find method that works with single id. Other than that, just use Enumerable http://ruby-doc.org/core/classes/Enumerable.html to get what you need.

Stephen Eley

unread,
Mar 9, 2010, 10:44:16 PM3/9/10
to mongo...@googlegroups.com
On Mon, Mar 8, 2010 at 3:28 AM, graf.otodrakula
<graf.ot...@gmail.com> wrote:
> 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?

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

Ckhrysze

unread,
Mar 10, 2010, 5:16:40 PM3/10/10
to MongoMapper
What about something like

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

Erica wilson

unread,
Jan 18, 2015, 1:03:28 PM1/18/15
to mongo...@googlegroups.com, graf.ot...@gmail.com

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

Reply all
Reply to author
Forward
0 new messages