0

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
    }
});
stack_pooper
  • 287
  • 1
  • 2
  • 12
  • What does the document look like? What does the `req` object look like when you `console.log(req)`? Both of these things are vital to the reason why you are not returning a match. Include the data requested in your question if you are still unsure. – Blakes Seven Apr 10 '16 at 05:42
  • @BlakesSeven Updated with schemas. The req.user.username correctly stores the username of the requestor. – stack_pooper Apr 10 '16 at 06:07
  • You are using a "refereced" schema, so "username" resides in another collection. The only available data in the array is an `ObjectId` referring to the other collection. – Blakes Seven Apr 10 '16 at 06:08
  • So you would need to call `.populate()` here in order to "pull in" the related data. Then you can "filter" out the non matching data from the "parent". Or alternately use [`$lookup`](https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/) with MongoDB 3.2 instead – Blakes Seven Apr 10 '16 at 06:11
  • Is `filter` a mongoose method or should I filter using `js`? – stack_pooper Apr 10 '16 at 06:17
  • Read the [duplicate](http://stackoverflow.com/questions/11303294/querying-after-populate-in-mongoose), it explains everything. That is the point of marking as a duplicate. Being that you actually read it. – Blakes Seven Apr 10 '16 at 06:20

0 Answers0