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

157
Views
¿Cómo obtengo el mismo resultado principal independientemente del orden de los elementos secundarios que se consultan? (Uno a muchos)

Estoy construyendo un chat con salas privadas. Lo que estoy tratando de hacer es encontrar una habitación a la que también pertenezcan dos usuarios. Si no hay uno, cree uno.

esquema de chat

 export const ChatSchema = new mongoose.Schema({ participants: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }], created_at: { type: Date, default: Date.now }, });

Consulta

 async findChatBetweenUsers(participantOneId, participantTwoId) { return await (await this.chatModel.findOne( { participants: [participantOneId, participantTwoId] } )).populate('participants'); }

Controlador

 async onJoinRoom(socket: Socket, reciever) { const authUser: User = await this.authUser(socket); const recievingUser: User = await this.userService.findOne(reciever.username); const chat = await this.chatService.findChatBetweenUsers(authUser, recievingUser); //Create new chat if doesn't exist if(Object.entries(chat).length === 0){ const newChat = await this.chatService.create(authUser, recievingUser); return; } console.log(chat) }

el problema que tengo

El orden de la auth y el receiver cambia según quién haya iniciado sesión y produce un resultado de consulta diferente para el chat. Por ejemplo:

Ejemplo uno

 const chat = await this.chatService.findChatBetweenUsers('KylesId', 'ChrisId');

Producción

 "chat" : { "_id": 'chatOneId', "participants": ['KylesId', 'ChrisId'] }

Ejemplo dos

 const chat = await this.chatService.findChatBetweenUsers('ChrisId','KylesId');

Producción

 "chat" : { "_id": 'chatTwoId', "participants": ['ChrisId', 'KylesId'] }

¿Cómo obtengo el mismo resultado a pesar de que se consulta el orden de los participants ?

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

0

Si, en cambio, desea encontrar una matriz que contenga los elementos "rojo" y "en blanco", sin tener en cuenta el orden u otros elementos de la matriz, utilice el operador $all:

 this.chatModel.find({ participants: {$all: [ObjectId('61ce732e33c7e8a9ad80e151'), ObjectId('61ccf3251b9ba5c8a6ecf2a3')]} });
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!