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
result