[Fixed] Check if user id is in mongoose document – if yes, change property value to true?


I have an expressjs router that looks in MongoDB collection using the mongoose findById method. It returns an object where inside there exist an userHasSelected array with users id. I dont want to return users id, but just check if current users (the one who made the request) exist in the array. If he is then return true instead of returning the user id.

The verifytoken middleware in the router adds a user id property to the request.That user id is available in the get router message – can i somehow pass that to the Mongoose schema ???

//My router

router.get('/challenge/:challengeId', verifyToken ,function (req, res){
    //+ Return one challenge and its options
    //- Check if userId is set to options and winner properties
    let userId = req.userId;
    let challengeId = req.params.challengeId;

    Challenge.findById(challengeId, (err, suc)=>{

// And the mongoose Schema

const mongoose = require('mongoose');

var Schema = mongoose.Schema;

//Optionsschema is use in the ChallengeSchema
var OptionSchema = new Schema({
    name: { type: String},
    isCorrect : { type: Boolean },
    description: { type: String },
    image : { type : String },
    userHasSelected : { type : Object, get : returnUserChallengeStatus}

OptionSchema.set('toObject', { getters: true });
OptionSchema.set('toJSON', { getters: true });

var ChallengeSchema = new Schema({
    shortEventId : String,
    organization: String,
    name: String,
    winner: String,
    options : [OptionSchema]

ChallengeSchema.set('toObject', { getters: true });
ChallengeSchema.set('toJSON', { getters: true });

ChallengeSchema.virtual('born').get(function(value) {
    return this.name + "plus andet"

module.exports = mongoose.model('challenge', ChallengeSchema);

So again – I dont want to return the user id from the userHasSelected array – just check if he is there and if yes, use a getter or a method to set value to true.

Updated explanation
The findById returns this object / document

    "shortEventId": "d2d3",
    "organization": "Braedstrup",
    "name": "1. december",
    "winner": "5b864cbaa9ce291b148ddd6d",
    "options": [
            "name": "Matas",
            "isCorrect": "true",
            "description": "Matas er byens førende indenfor pleje og Matas er byens førende indenfor pleje og omsorg Matas er byens førende indenfor pleje og omsorg",
            "image": "https://cityxpstorage.blob.core.windows.net/images/Matas.png",
            "userHasSelected": [
                    "userId": "5b864cbaa9ce291b148ddd6d"
            "name": "Føtex",
            "isCorrect": "false",
            "description": "Føtex er en dejlig butik",
            "image": "https://cityxpstorage.blob.core.windows.net/images/Føtex.png"
            "name": "Kvickly",
            "isCorrect": "false",
            "description": "Kvickly er en dejlig butik",
            "image": "https://cityxpstorage.blob.core.windows.net/images/Matas.png"
            "name": "MC Jørgensen",
            "isCorrect": "false",
            "description": "MC Jørgensen er en dejlig butik",
            "image": "https://cityxpstorage.blob.core.windows.net/images/Matas.png"
    "startDate": "2018-10-06T00:00:00.000Z",
    "endDate": "2018-10-06T23:59:00.000Z"

So the nested array ‘userHasSelected’ contains information about the users id. I do not want to send that – instead I would like to a {userId : true}.
I have read that getters a able to handle outgoing data.

Posible Solution

I could make the check inside the router get method before returning the object to the client like this

// If user is in array set user to true. I would like to move this responsibility to the schema / document.
suc.options.forEach(option => {
    option.userHasSelected = {userId : true}

But I would really like schema to be responsible for that – Is that possible?


I had similar issue and found a workaround. Simply create an optional field on your responsible modal schema, let call it "status". On your controller, check if your array includes requested user’s id and write to that field. For example;

on your schema;

    caseStatus: {
        type: Boolean,
        required: false
    voters: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',

then on your controller;

    let theCase = await Case.find({speciality: res.locals.user_speciality }).exec();

            let status = theCase.voters.includes(res.locals.user_id);
            caseItem.caseStatus = status;

Leave a Reply

(*) Required, Your email will not be published