Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda