'Mongoose can't find capital letter fields

I have a schema in Mongo like this:

const userSchema = new mongoose.Schema({
  _id: {
    type: mongoose.Types.ObjectId,
    required: true,
  },
  organizationId: {
    type: mongoose.Types.ObjectId,
    required: true,
  },
  projectsId: {
    type: [mongoose.Types.ObjectId],
    default: []
  },
  roleId: {
    type: mongoose.Types.ObjectId,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  pic: {
    type: String,
    default: ''
  },
  createdOn: {
    type: Date,
    default: Date.now
  }

I want to find users with a specific organizationId, but mongoose returns empty array. When i try to find with an _id, email it works but when i try it with organizationId and projectsId it doesn't find it. This led me to believe there is a problem with field names which have capital letters in them.

Here is my node/express code:

const getMemberListController = async (req, res, next) => {
  try {
    const memberList = await User.find({organizationId: '6239b5fc3c16a8041341a3e8'});
    res.status(200).json(memberList);
  } catch (error) {
    res.status(500).json(error);
  }
}

The above returns an empty array, eventhough

 User.find({_id: '623ad5c75d17751ff664a9ae'})

returns:

[
  {
    "pic": "",
    "_id": "623ad5c75d17751ff664a9ae",
    "organizationId": "6239b5fc3c16a8041341a3e8",
    "roleId": "ba3e35fc1341a80462c16398",
    "projectsId": [
      "6239b61f8f8e67d675297178",
      "6239b637a4ec3ff5ef2df0cb"
    ],
    "email": "[email protected]",
    "password": "$2a$10$hIMWjl2wz9g1C3NN4sqwHOMgx0epaJtdOmGVWckkRcCqltIn9JlTe",
    "createdOn": "2022-03-23T10:42:11.170Z"
  }
]

I tried it in mongoDB locally by running the same code: db.users.find({organizationId: '6239b5fc3c16a8041341a3e8'}) but this time, it returns the specified user.

I don't know if it is the capitalization or mongoose the issue. Please help. Thanks.



Solution 1:[1]

Found it.

The issue was when i was creating users in db, i was using this script:

db.users.insertMany([
  {
  'organizationId': '3c16a8046239b5fc1341a3e8',
  'roleId': 'ba3e35fc1341a80462c16398',
  'email': '[email protected]',
  'password': '$2a$10$hIMWjl2wz9g1C3NN4sqwHOMgx0epaJtdOmGVWckkRcCqltIn9JlTe'
  },
  {
  'organizationId': 'b5fc3c16a80462391341a3e8',
  'roleId': 'ba3e35fc1341a80462c16398',
  'email': '[email protected]',
  'password': '$2a$10$hIMWjl2wz9g1C3NN4sqwHOMgx0epaJtdOmGVWckkRcCqltIn9JlTe'
  },

])

But this will make the foreign key fields(organizationId roleId and projectsId) strings. Thus failing the check.

The script should be:

db.users.insertMany([
  {
  'organizationId': new ObjectId('3c16a8046239b5fc1341a3e8'),
  'roleId': new ObjectId('ba3e35fc1341a80462c16398'),
  'email': '[email protected]',
  'password': '$2a$10$hIMWjl2wz9g1C3NN4sqwHOMgx0epaJtdOmGVWckkRcCqltIn9JlTe'
  },
  {
  'organizationId': new ObjectId('b5fc3c16a80462391341a3e8'),
  'roleId': new ObjectId('ba3e35fc1341a80462c16398'),
  'email': '[email protected]',
  'password': '$2a$10$hIMWjl2wz9g1C3NN4sqwHOMgx0epaJtdOmGVWckkRcCqltIn9JlTe'
  },
])

Now mongoose will check ObjectId to ObjectId instead of ObjectId to String.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joseph