I'm having issues with finding a specific user in a reference array, I have an Activity schema which as an array of participants that references a User Schema.
I've been trying using something like this:
(userid is a variable has the specified user id)
activityQuery = Activity.find({participants: userid});
it does not return anything.
Example of the data in the db:
Here are the used schemas:
Activity Model
const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const activitySchema = mongoose.Schema({
image: { type: String, required: true },
content: { type: String, required: true },
title: { type: String, required: true },
numberOfParticipants: { type: Number, required: true },
state: { type: String, required: true },
date: { type: Date, required: true },
participants: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
areasOfInterest: [
{
type: String,
required: true,
},
],
inquiries: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Inquiry",
},
],
proposalId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Proposal",
},
});
activitySchema.plugin(uniqueValidator);
module.exports = mongoose.model("Activity", activitySchema);
User Model
const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const userSchema = mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
userType: { type: String, required: true },
birthdate: { type: Date },
name: { type: String, required: true },
state: { type: String, required: true },
profileImage: { type: String },
gender: { type: String },
address: { type: String },
website: { type: String },
accountableName: { type: String },
areasOfInterest: [
{
type: String,
},
],
activities: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Activity",
},
],
member: { type: String },
obs: { type: String },
reasons: [{ type: String }],
formation: { type: String },
school: { type: String },
phone: { type: String },
familyMembers: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "FamilyMember",
},
],
photos: [{ type: String }],
joinDate: { type: Date },
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model("User", userSchema);