0

This is about displaying documents of a collection based on the logged in user's role/team. Please help me correct the part of the query which uses $in operator.

The user model includes below fields apart from others:

  userName: { type: String, unique: true, required: true },
  manager: String,
  isManager: Boolean,
  managesAll: Boolean,
  team: [{
    userId: String,
    userName: String,
    isManager: Boolean
  }],

There is a collection 'servicelog'. One of the fields of the servicelog document is "execName". When the list of servicelog is displayed, depending on the logged in user the log shd be displayed in one of the three ways:

  if(user.role === 'Admin' || user.managesAll === true) {
    // display all documents
    docQuery = ServiceLog.find().collation({ locale: 'en'});

  } else if(user.isManager === true) {
    // display where execName is userName or one of the userName in user.team
    docQuery = ServiceLog.find({
      $or: [
        { execName: user.username },
        { execName: { $in:  user.team } },  // PLS HELP CORRECT THIS PART. How do I look in user.team.userName
      ],
      });

  } else if(user.isManager === false) {
    // display where execName is userName 
    docQuery = ServiceLog.find({ execName: user.username });

  }


1 Answer 1

1

Assume that your user.team is an array, you can do something like:

{ execName: { $in:  user.team.map(u => u.userName) } }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.