How to filter by population result in mongoose?

36 views
Skip to first unread message

Rouben Shekspir

unread,
Jul 30, 2015, 6:39:55 AM7/30/15
to Mongoose Node.JS ODM
Hi folks.
Let's consider this situation.I have two mongodb schemas.

    var ChildSchema = new Schema({
        name: String,
        place: {
            type: Schema.Types.ObjectId,
            ref: 'Parent',
            index: true,
            required: true
        }
    });

    var ParentSchema = new Schema({
        name: String
    });

So now I want to get all ChildSchema documents thats parent name is 'bar'. Also I'm using skip,limit, so there will be no case for using manual filtering in Node(it will crash everything).

Jason Crawford

unread,
Jul 30, 2015, 10:58:30 AM7/30/15
to mongoo...@googlegroups.com
Sounds like you're trying to do a join. I don't think you can do that in Mongo. Instead, you can find all parents whose name is 'bar', then do a second query to find all the children who reference those parents' ids.

Hope that helps,
Jason

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages