How to Populate not returning nulls

87 views
Skip to first unread message

Mauricio Mercado

unread,
Aug 14, 2013, 11:14:49 AM8/14/13
to mongoo...@googlegroups.com
Hello,

I have the following schema

User {
_id
name
screencolor
username
}

Post {
_id
comment
author
replyTo
nextSibling
previousSibling
topReplies
}


Since Im saving an entire conversation, if a post has a replyTo (parent) attribute set, it will probably have either previous Sibling or next Sibling, and it might have top replies, but if it has no parent it will have no siblings.

Im doing the following query:

Post.find( { draft: false } ).sort({createdAt: -1})
  .populate({path: 'previousSibling nextSibling topReplies replyTo', select: '_id author'})
  .populate({path: 'author', select: 'username screencolor'})
  .exec(function(err, posts) {
    if(err){
      return next(new restify.ConflictError(err));
    }
    if (posts.length === 0) {
      return next(new restify.NotFoundError("No Posts Found"));
    } else {

      User.populate(posts, {path: 'previousSibling.author nextSibling.author topReplies.author replyTo.author', select: 'username name screencolor'}, function(err, docs) {
        res.json(200, docs);
        return next();
      });
    }
  });
};

Now its understandable from this post that the intended way that mongoose works is that populates returns null if the field requested does not return anything, but is there a way to not return null fields at all, or return something else?

Thank you!

Mauricio Mercado

unread,
Aug 14, 2013, 12:14:27 PM8/14/13
to mongoo...@googlegroups.com
Im using mongoose 3.6.16

Aaron Heckmann

unread,
Aug 14, 2013, 5:45:20 PM8/14/13
to mongoo...@googlegroups.com

On Wed, Aug 14, 2013 at 8:14 AM, Mauricio Mercado <m...@maumercado.com> wrote:
Now its understandable from this post that the intended way that mongoose works is that populates returns null if the field requested does not return anything, but is there a way to not return null fields at all, or return something else?

There is not.


--
Aaron


Reply all
Reply to author
Forward
0 new messages