Populate Query Filtering

6,454 views
Skip to first unread message

Mohamed Mansour

unread,
Oct 16, 2011, 11:28:59 PM10/16/11
to mongoo...@googlegroups.com
Hello,

According to https://github.com/LearnBoost/mongoose/pull/553 this feature has been implemented by querying populate. I have updated mongoose to version 2.3.3

Topic= new Schema({
  name: String,
  slug: String
});

Foo = new Schema({
  name: String,
 _topics: [ { type: ObjectId, ref: 'Topic' } ]
});

When I do a basic query like this: Foo.find().populate('_topics')

I get the following results:

[
  {
    name: 'A',
    _topics: [
       {
          slug: 'technology',
          _id: '4e9b9b88196415a47200000a'
        }
    ]
  },

  {
    name: 'B',
    _topics: [
       {
          slug: 'cooking',
          _id: '4e9b9b88196415a472000006'
        }
    ]
  }
]

Now when I query for populate with:  Foo.find().populate('_topics', {slug: 'cooking'})

It gets all the two topics, it doesn't filter them with just cooking. Is this supported in Mongoose, if so, how can I do it.

Thanks,
Mohamed Mansour

 

Aaron Heckmann

unread,
Oct 17, 2011, 11:03:46 AM10/17/11
to mongoo...@googlegroups.com
Foo.find().populate('_topics', null, {slug: 'cooking'})

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Mohamed Mansour

unread,
Oct 17, 2011, 11:57:17 AM10/17/11
to mongoo...@googlegroups.com
Hi Aaron,

Can you explain what that does exactly? It seems it "filters" the topics array, not the entire query. My objectives is only finding Foo's who have cooking as their topic.

When doing, the following, it always gives me an empty array:

Foo.find({'_topics.slug': cooking}

So it just filters the populate, not the entire query, any idea how to make it work for the entire query? 

Kind regards,
Mohamed Mansour

Aaron Heckmann

unread,
Oct 17, 2011, 1:58:50 PM10/17/11
to mongoo...@googlegroups.com
Yes this filters the array, not the entire query. Making this work on the entire query requires loading your entire Foo data set into memory and is obviously not recommended. 

Foo
.find() // load all Foos
.populate('_topics', null { slug: 'cooking' }) // after all Foos are loaded into memory, also load each Foos _topics where slug == cooking
.run(callback)

Mohamed Mansour

unread,
Oct 17, 2011, 2:16:23 PM10/17/11
to mongoo...@googlegroups.com
Thanks Aaron,

I believe the better approach is not using populate in this case and instead of using slugs I will use _ids in the webapp. If I were going to use slugs (for a url), then it would make sense to call mongo twice (get the id from the topic, then use that for the query).

Is that a mongoose limitation, or just not implemented yet to filter the entire query based where keys are populated. It populates while scanning each row? So if I were to submit a patch to mongoose where it checks if the key matches the populate, would that be accepted?

Thanks, 

Aaron Heckmann

unread,
Oct 17, 2011, 5:32:20 PM10/17/11
to mongoo...@googlegroups.com
On Mon, Oct 17, 2011 at 2:16 PM, Mohamed Mansour <m0.inte...@gmail.com> wrote:
Is that a mongoose limitation, or just not implemented yet to filter the entire query based where keys are populated. It populates while scanning each row? So if I were to submit a patch to mongoose where it checks if the key matches the populate, would that be accepted?

Its not possible in mongoose. What you propose is running the queries directly on mongodb - which may involve $eval or map reduce or something else.

--
Aaron


santhosh h

unread,
Dec 22, 2015, 1:18:41 AM12/22/15
to Mongoose Node.JS ODM
In mongoDb we can have more then one populate.If this is Implemented  this may limit other populate so mongo authors did it like this i think. But i was also looking to have filter like this in my application. they should provide some solution. 

Micheal Olawale

unread,
Jan 16, 2023, 10:09:00 AM1/16/23
to Mongoose Node.JS ODM
Please is there any update on this?
Reply all
Reply to author
Forward
0 new messages