Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda