代码之家  ›  专栏  ›  技术社区  ›  Nikos Chatzivasileiadis

如何从Mongoose填充查询中排除空值

  •  1
  • Nikos Chatzivasileiadis  · 技术社区  · 7 年前

    我正在构建一个应用程序,我已经创建了两个模型。

    const userschema=new schema({
    _ id:schema.types.objectid,
    帐户:{
    类型:字符串,
    独特:真实
    }
    电子邮件:字符串,
    名字:string,
    姓氏:字符串
    }
    
    const venueschema=新建架构({
    _ id:schema.types.objectid,
    场地类型:字符串,
    容量:个
    })
    < /代码> 
    
    

    const mediateschema=new schema({
    _ id:schema.types.objectid,
    帐户:{
    类型:字符串,
    独特:真实
    }
    用户:类型:schema.types.objectid,
    参考文献:用户
    }
    地点:类型:schema.types.objectid,
    裁判:“场地”
    }
    })
    < /代码> 
    
    

    创建中介模式是为了填充多个路径。

    问题是当我尝试创建一个类似

    var populatequery=[path:'user',match:account:'testuser1',select:'email',path:'venue',match:venue_type:'club',select:'venue type'];
    
    const confirmedhouses=等待调解人。查找()
    .存在(“地点”,真实)
    .Populate(填充查询)
    执行();
    < /代码> 
    
    

    返回的数组包含匹配未完成时具有空值的对象。

    例如,当我使用前面的匹配项进行查询时,会得到以下结果

    所以我想要的是,当一个用户或者地点或者什么东西是空的(所以匹配没有完成),整个对象就不会被返回。

    我有以下解决方案,但我不想这样做

    var i=confirmedguenses.length;
    (i)- {
    if(confirmed场馆[i].user==null){
    温度拼接(I,1)
    }
    }
    < /代码> <

    const MediatorSchema = new Schema({
        _id: Schema.Types.ObjectId,
        account:{
          type: String,
          unique: true
        },
        user: {type: Schema.Types.ObjectId, 
          ref:'User'
        }
        venue: {type: Schema.Types.ObjectId, 
          ref:'Venue'
        }
      })
    

    创建中介模式是为了填充多个路径。

    问题是当我尝试创建一个类似

    var populateQuery = [{path:'user',match: { account:'testuser1'},select:'email'},{path:'venue',match: { venue_type: 'club'}, select:'venue_type'}];
    
    const confirmedVenues = await  Mediator.find({})  
    .exists('venue',true)
    .populate(populateQuery)
    .exec();
    

    返回的数组包含匹配未完成时具有空值的对象。

    例如,当我使用前面的匹配项进行查询时,会得到以下结果

    enter image description here

    所以我想要的是,当一个用户或者地点或者什么东西是空的(所以匹配没有完成),整个对象就不会被返回。

    我有以下解决方案,但我不想这样做

    var i =confirmedVenues.length;
    while(i--){
      if(confirmedVenues[i].user == null){
        temp.splice(i,1)
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Nikos Chatzivasileiadis    7 年前

    最后,我必须在这里使用聚合,没有其他选择。

    模式不需要更改

    var results =await dbo.collection('mediators').aggregate([
         { $lookup:
             {
               from: 'venues',
               localField: 'venue',
               foreignField: '_id',
               as: 'venue'
             }
          },
            $match:{$and:[{"venue.venue_type":req.query.venue_type} , {"venue.capacity":{$gte:parseInt(req.query.capacitylb) , $lte:parseInt(req.query.capacityub)}}]}
          },{
            $lookup:
             {
               from: 'users',
               localField: 'user',
               foreignField: '_id',
               as: 'user'
             }
          },{
            $lookup:
            {
               from: 'professionals',
               localField: 'professional',
               foreignField: '_id',
               as: 'professional'
            }
          },{
            $lookup:
            {
              from:'availabilities',
              localField: 'availability',
              foreignField: '_id',
              as: 'availability'
            }
          },{
            $unwind: '$availability'
          },{
            $match:{$and:[{"availability.start":{$lte:new Date(req.query.dateFrom)}},{"availability.end":{$gte:new Date(req.query.dateTo)}}]}
          },{
            $lookup:
            {
              from:'locations',
              localField: 'location',
              foreignField: '_id',
              as: 'location'
            }
          },{
            $project:{
              "_id":1,
              "email":"$user.email",
              "organization_name":"$user.organization_name",
              "website":"$user.website",
              "profile_pic_hash":"$user.profile_pic_hash",
              "bio_hash":"$user.bio_hash",
              "venue_type":"$venue.venue_type",
              "capacity":"$venue.capacity",
              "flat_fee":"$professional.flat_fee",
              "per_participant_fee":"$professional.per_participant_fee",
              "hourly_fee":"$professional.hourly_fee",
              "start_date":"$availability.start",
              "end_date":"$availability.end",
              "distance":"$dist.calculated",
              "location":"$location.location.coordinates",
              "country":"$location.country",
              "city":"$location.city",
              "street":"$location.street",
              "number":"$location.number",
              "zip":"$location.zip"}}
    

    它非常适合我。谢谢 Anthony Winzlet 谢谢你的帮助。