Filtering by referenced document property in the MongoDB

Taking advantage of the “1 to N bidirectional relationship in the MongoDB” post, I intend to show you how to “make a cross join” with MongoDB and filter by a referenced document property.

I’ll use all that code and add another endpoint to the API. This new endpoint will GET the comments where the BlogPost title contains some word.

Let’s go to code:

router.get('/comments/:page/:blogPostTitle', function(req, res) {
        // join BlogPost collection
            $lookup: {
                from: 'blogposts', // special attention here, it is the name of the collection
                localField: 'BlogPost',
                foreignField: '_id',
                as: 'BlogPost'
        // convert array of BlogPost to object
            $unwind: '$BlogPost'
        // filter
                "BlogPost.Title": new RegExp(req.params.blogPostTitle) 
        // pagination info
        { $sort: { "BlogPost.Title": 1 } },
        { $skip: ( * 10) - 10 }, // (page index * page size) - page size
        { $limit: 10 }
        .exec(function(err, entities) {
            if (err)


Now I’ll explain some points:

  1. This endpoint will receive “page” and “blogPostTitle” parameter.
  2. We will use the “aggregate()” method instead of “find()”.
  3. In the “$lookup” we will relate the property “BlogPost” with de collection of documents “blogposts”. Pay attention in the capitalizations here! The collection name has to be the same that you will find into mongodb.
  4. The “$lookup” will build an object. “$unwind” will transform that in an array!
  5. Now we are able to filter a property from that array.
  6. For curiosity I added a pagination and sort mechanism.
  7. And I also use RegExp, so is it possible build complex regular expressions to filter.

And now, the result:

I think the scenarios like that can be common, and I hope that tip can be useful.

I hope you enjoyed it!



With more than 15 years of experience developing softwares and technologies, talking about startups, trends and innovation, today my work is focused to be CTO, Software Architect, Technical Speaker, Technical Consultant and Entrepreneur.

From Brazil, currently lives in Portugal working at as Software Architect, besides to keep projects in Brazil, like


80 thoughts on “Filtering by referenced document property in the MongoDB”

  1. Pingback: SEO Consultants
  2. Pingback: Google
  3. Pingback: 主页
  4. Pingback: __ds11
  5. Pingback: Tesla Sucks
  6. Pingback: read an article
  7. Pingback: tutorialcup
  8. Pingback: click to go
  9. Pingback: as reported here
  10. Pingback: provided link
  11. Pingback: as reported here
  12. Pingback: link
  13. Pingback: click to read more
  14. Pingback: forex signals
  15. Pingback: Cheap hoverboard
  16. Pingback: essayforme
  17. Pingback: Cialis generic
  18. Pingback: Cialis 20mg
  19. Pingback: Cialis 5 mg
  20. Pingback:
  21. Pingback: follow this link
  22. Pingback: view more
  23. Pingback:
  24. Pingback:
  25. Pingback:

Leave a Reply