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

327
Vistas
Mongodb Searching in an array of ids using include does not work

I have this model:

const NeighborSchema = new Schema({
  friends: [
    {
      type: Schema.Types.ObjectId,
      ref: "users",
    },
  ],
  date: {
    type: Date,
    default: Date.now,
  },
});

module.exports = Neighbor = mongoose.model("neighbor", NeighborSchema);

I am trying to see if a friend exists in friends of all neighbors:

const mongoose = require("mongoose");
const ObjectId = mongoose.Types.ObjectId;
const testIncludes = async () => {
  let neighbors = await Neighbor.find();
  let friends_ids = [];

  neighbors.map((neighbor) => {
    const { friends } = neighbor;
    friends_ids = [...friends_ids, ...friends];
  });

  // Returns false for this
  const element_to_search = ObjectId("60dcbb29118ea36a4f3ce229");
  // Returns false for this
  // const element_to_search = "60dcbb29118ea36a4f3ce229";
  let is_element_found = friends_ids.includes(element_to_search);
};
// Returns false in both cases
testIncludes();

Even though, element_to_search was taken directly from list of returned friends_ids array, when I try to search it using include, it returns false for some reason, whether I search it as a String or as an ObjectId.

Any idea what's going on?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Array.prototype.includes compares each element against the sample until it finds a match. Objects are considered equal only if they reference the same instance of the class. When you call a constructor const element_to_search = ObjectId("60dcbb29118ea36a4f3ce229"); it creates a new instance which has never been in the array, even if its value is the same.

You need to compare scalars. Strings for example:

friends_ids.map(f => f.toString()).includes("60dcbb29118ea36a4f3ce229");

or cast it strings when you build up the friends_ids at the first place to avoid the extra loop over the array.

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