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

140
Vistas
mongodb convert data to string. how to fix?

I want to get data from my database to work with variables.

Here is my database query:

db.progress.find({username:socket},function(err,res){

what do I get information on:

[ { _id: 61e180b54e0eea1454f8e5e6,
    username: 'user',
    exp: 0,
    fraction: 'undf'} ]

if i send a request

db.progress.find({username:socket},{exp:1},function(err,res){

then in return I will get

[ { _id: 61e180b54e0eea1454f8e5e6, exp: 0 } ]

how do i extract only 0 from the received data. I would like to do something like this.

var findprogress = function(socket,cb){
db.progress.find({username:socket},function(err,res){
cb(res.exp,res.fraction)
})
}



findprogress(socket,function(cb){
console.log(cb.exp)//0
console.log(cb.fraction)//undf
           })

but I don't know how to implement it correctly.

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

0

I recommend aggregate in this case so you can more easily manipulate your projected data. For example:

db.progress.aggregate([
{ $match: { username: socket } },
{ $project: { _id: 0, exp: 1 } }
])

This way you directly tell the query to not include the objectId, which is typically included by default.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try to give 0 to _id:

db.progress.find({username:socket},{exp:1 , _id:0},function(err,res){
about 4 years ago · Juan Pablo Isaza Denunciar

0

find returns an array, so you will need to select the array element before accessing the field:

var findprogress = function(socket,cb){
  db.progress.find({username:socket},function(err,res){
    cb(res[0].exp,res[0].fraction)
  })
}
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