Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Return record if matches exact value using mongodb

model.js

import mongoose from 'mongoose';
const { Schema, Types } = mongoose;

const participants = {
  user_id: Types.ObjectId(),
  isAdmin: Boolean
}

const groupSchema = new Schema({
  id: Types.ObjectId(), // String is shorthand for {type: String}
  name: String,
  users: [participants]
});

export const Group = mongoose.model('Group', groupSchema);

query.js

 export const checkUser = async(groupprops) => {
  await return group.findOne({'participants.user_id': { $all: groupprops } }).exec();
}; // groupprops is array of user_id = ['123', '456'] like this

I want to filter array if the same exact value matches record it must return that only else it must not return record like if I have 3 users of id [123, 456, 789] and I search for users [123, 456] then it must not return any record until I did not enter record that matches exact. I am getting issue as it only checks of [123, 456] exist then it matches the record ignore other values but it must exact matches the user_id

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use aggregation operator $setEquals to check both the array is equals,

return await group.findOne({
  $expr: {
    $setEquals: ["$users.user_id", groupprops]
  }
}).exec();

Playground

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!