I have a Project
schema which has a field named collaborators
. The collaborators
field is an array of User
objects. The User
objects contain a field called username. I would like to find all projects that have a collaborator
has the same username as the requestor's
username. Heres the query I've come up with but it returns nothing.
Project.find({ 'collaborators.username': req.user.username })
.
Please let me know if I can provide any other helpful information.
Project Schema
var ProjectSchema = new Schema({
created: {
type: Date,
default: Date.now
},
creator: {
type: Schema.ObjectId,
ref: 'User'
},
description: {
type: String,
default: ''
},
name: {
type: String,
default: '',
required: 'Please fill Project name',
trim: true
},
phase: {
type: Number,
default: ''
},
sponsor: {
type: String,
default: '',
trim: true
},
collaborators: [{
type: Schema.ObjectId,
ref: 'User'
}],
emailInvitees: {
type: Array,
default: []
},
userInvitees: {
type: Array,
default: []
},
comments: {
type: String,
default: ''
}
});
User Schema
var UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
organization: {
type: String,
trim: true,
default: '',
required: 'Please fill in an organization name'
},
position: {
type: String,
trim: true,
default: '',
required: 'Please fill in the title of your position'
},
displayName: {
type: String,
trim: true
},
email: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your email'],
match: [/.+\@.+\..+/, 'Please fill a valid email address']
},
username: {
type: String,
unique: 'testing error message',
required: 'Please fill in a username',
trim: true
},
password: {
type: String,
default: '',
validate: [validateLocalStrategyPassword, 'Password should be longer']
},
salt: {
type: String
},
provider: {
type: String,
required: 'Provider is required'
},
providerData: {},
additionalProvidersData: {},
roles: {
type: [{
type: String,
enum: ['user', 'admin']
}],
default: ['user']
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
/* For reset password */
resetPasswordToken: {
type: String
},
resetPasswordExpires: {
type: Date
}
});