Mongoose query where value is not null

前端 未结 6 1346
轻奢々
轻奢々 2020-12-23 15:55

Looking to do the following query:

Entrant
    .find
      enterDate : oneMonthAgo
      confirmed : true
    .where(\'pincode.length > 0\')
    .exec (er         


        
6条回答
  •  被撕碎了的回忆
    2020-12-23 16:21

    Ok guys I found a possible solution to this problem. I realized that joins do not exists in Mongo, that's why first you need to query the user's ids with the role you like, and after that do another query to the profiles document, something like this:

        const exclude: string = '-_id -created_at -gallery -wallet -MaxRequestersPerBooking -active -__v';
    
      // Get the _ids of users with the role equal to role.
        await User.find({role: role}, {_id: 1, role: 1, name: 1},  function(err, docs) {
    
            // Map the docs into an array of just the _ids
            var ids = docs.map(function(doc) { return doc._id; });
    
            // Get the profiles whose users are in that set.
            Profile.find({user: {$in: ids}}, function(err, profiles) {
                // docs contains your answer
                res.json({
                    code: 200,
                    profiles: profiles,
                    page: page
                })
            })
            .select(exclude)
            .populate({
                path: 'user',
                select: '-password -verified -_id -__v'
                // group: { role: "$role"} 
              })
        });
    

提交回复
热议问题