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

75
Vistas
Script that returns _id in MongoDB with JavaScript

I need a javascript code that saves in a variable the _id I'm having trouble getting that _id because it’s inside an array.

I have looked everywhere but I can't find a solution for this.

  {
    "_id": {
      "$oid": "626bacea1847f675e47b2bd8"
    },
    "tipo": "conta",
    "data_abertura": {
      "$date": "2013-02-19T00:00:00Z"
    },
    "observacoes": "Sem observações",
    "iban": "PT50000506515926456299903",
    "saldo": 1456.23,
    "bic": "BBPIPTPL001",
    "tipo_conta": "Conta Ordenado",
    "cartoes": [
      {
        "_id": {
          "$oid": "626bacea1847f675e47b2bd7"
        },
        "num_cartao": 4908509005925727,
        "nome_cartao": "João Correia",
        "validade": {
          "$date": "2025-10-03T00:00:00Z"
        },
        "pin": 5609,
        "cvc": 975,
        "estado": "Ativo",
        "tipo_cartao": "Crédito"
      }
    ],
    "permissoes": {
      "levantamentos": true,
      "depositos": true,
      "pagamentos": true,
      "transferencias": true,
      "creditos": true,
      "acoes": true
    },
    "titulares": [
      {
        "$oid": "626bacea1847f675e47b2bd6"
      }
    ]
  }

I want the cartoes _id

"cartoes": [
      {
        "_id": {
          "$oid": "626bacea1847f675e47b2bd7"
        },

I want something like this:

let conta = db.banco.findOne({"tipo": "conta"});
idConta = conta._id;

console.log("id: " + idConta);//id: 626bacea1847f675e47b2bd8
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Edit:

According to the comments, I understand you want to input num_cartao value and to get an output of the _id of this cartao. You can use an aggregation for this:

const cartao_id = await db.collection.aggregate([
  {
    $match: {cartoes: {$elemMatch: {"num_cartao": 4908509005925727}}}
  },
  {
    $project: {
      cartoes: {
        $filter: {
          input: "$cartoes",
          as: "item",
          cond: {$eq: ["$$item.num_cartao", 4908509005925727]}
        }
      }
    }
  },
  {
    $project: {
      data: {"$arrayElemAt": ["$cartoes", 0]}
    }
  },
  {
    $project: {
      _id: {$toString: "$data._id"}
    }
  }
])

console.log(`cartao_id: ${cartao_id}`);//id: 626bacea1847f675e47b2bd7

That will provide you what you want, as you can see on this playground

You first $match the documents with the right num_cartao, then $filter the right carto from the cartoes array, and then you format it to string.

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