Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda