I'm currently working on an app where I've defined 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 purpose of the mediator schema is to link multiple paths together.
The issue arises when attempting to perform 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 resulting array contains objects with null values if the specified match criteria are not met.
For instance, with the provided matches, the results look like this:
https://i.sstatic.net/Jz3sR.png
What I am aiming for is that if a user or venue (or any other criteria) is NULL, the entire object should not be returned.
I did come up with a solution, but I'm looking for a better approach:
var i =confirmedVenues.length;
while(i--){
if(confirmedVenues[i].user == null){
temp.splice(i,1)
}
}