I am building an app and I have create 2 models.
const UserSchema = new Schema({
_id: Schema.Types.ObjectId,
account:{
type: String,
unique: true
},
email: String,
first_name: String,
last_name: String
}
const VenueSchema = new Schema({
_id: Schema.Types.ObjectId,
venue_type: String,
capacity: Number
})
and
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'
}
})
the mediator Schema is created in order to populate multiple paths .
The problem is that when i try to create a query like
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();
the returned array contains objects with null values when the match is not fulfilled.
For example when I query with the previous matches I get the following results
So what I want is , when a user or venue or something is NULL ( so the match is not fulfilled) , the Whole object not to be returned.
I got the following solution but i dont want to do it this way
var i =confirmedVenues.length;
while(i--){
if(confirmedVenues[i].user == null){
temp.splice(i,1)
}
}
$lookup
aggregation here... No other option – Viburnum