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

138
Views
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 answers
Answer question

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 Report

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 Report

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 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!