0

I am using NodeJs and MongoDb as a back-end service.I have several documents in my collection having field named _id and Name.

I want to get Output in Json objects like below:

[ 
  {   
    Name:"Paul"
  },
  {   
    Name:"Jon"
  }
] 

Here is my code:

var express = require('express');    
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true})); 

app.post('/offers',(req, res) => {

MongoClient.connect(url, (err, db) => {           
if(err) throw err;
var dbo = db.db('Tiffino_db');

dbo.collection("Offers")
    .find({},{ projection: { _id: 0 } })
    .toArray((err, result) => {
         if (err) {
                 console.log("Error:", +err);
             }
             else { 
                 output =  result.map(r => r.Name);
                 res.json({"Name":output});
                 db.close();
            }
       });
   });
});

Here is my Output:

{
"Name": [
    "Paul",
    "Jon",
    "David",
    "Aina"
  ]
}

Please let me know how to modify code to get desired output.

THANKS

6
  • 2
    "I want to get Output in Json objects like below" — That is not JSON. Commented Oct 16, 2018 at 13:35
  • Consider using mongoose package mongoosejs.com it will make your life a lot easier working with mongodb Commented Oct 16, 2018 at 13:35
  • @kay how can i get using Mongodb driver Commented Oct 16, 2018 at 13:37
  • Can u tell me what exactly is inside result Commented Oct 16, 2018 at 13:37
  • 1
    @ArayniMax is right, you need to know what result has and then probably loop it to obtain the Array you desire. Commented Oct 16, 2018 at 13:39

3 Answers 3

2

Instead of this

res.json({"Name":output})

Use this code

var json=    output.map(element=>{
    return {"Name":element.Name};
});
Sign up to request clarification or add additional context in comments.

5 Comments

element.Name, else you return the entire query result under the name field instead of only the name string.
Sory I forgot it
@Shilly you are right, your code using arrow function of ES6 and my code doesn’t
Your code uses an arrow as well, the full specific ES5 version is var json= output.map(function(element){ return {"Name":element.Name}; }); without an arrow. Anyways, take care.
Hehehe @Shilly you are right again but this time I know that,
1

Instead of:

output = result.map(r => r.Name); res.json({"Name":output});

Try:

output = result.map( r => ({ "Name": r.Name })); res.json( output );

As written, you map all the resulting records into one array, then assign that array to the property name. Instead you want to create a new object with the property name every time and return that array.

1 Comment

Thanks Shilly :), it gets result what i wanted.
0

I tested the code you provided. With the collection structure and the query if you remove the line output = result.map(r => r.Name); and just return result you will get the structure:

[{ "Name": "TestName" }, { "Name": "TestNam2" }]

Then the code inside the else block would look like this:

res.json(result);
db.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.