ASAD ALI
ASAD ALI

Reputation: 3

how to get key value from json result in node js

how can I get specify email and its value only, from JSON array result which should be like=>only( email: [email protected])

here is my code:

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("Scream_It");
  var query = { bid_location : 'abbottabad' };
  dbo.collection("bid_placement").find(query).sort({bid_amount:-1}).limit(1).toArray(function(err, result){
    if (err) throw err;
console.log(result);
      // console.log(JSON.stringify(result));
       var string = JSON.stringify(result);
       console.log(string);
        var objectValue = JSON.parse(string);
       // console.log(objectValue);
       console.log(objectValue.email);

this is the result which i am getting in console

[ { _id: 5a9f8849fc49ca1ff4aee3dc,
    email: '[email protected]',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' } ]

Upvotes: 0

Views: 1113

Answers (3)

Ivan Vasiljevic
Ivan Vasiljevic

Reputation: 5718

You could use select method from mongoose api. Basically, you can control with it what will result object contains of its properties. So, your code could look like this:

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("Scream_It");
  var query = { bid_location : 'abbottabad' };
  dbo.collection("bid_placement").find(query).select({email: 1, _id: 0}).sort({bid_amount:-1}).limit(1).toArray(function(err, result){
    if (err) throw err;
console.log(result);
      // console.log(JSON.stringify(result));
       var string = JSON.stringify(result);
       console.log(string);
        var objectValue = JSON.parse(string);
       // console.log(objectValue);
       console.log(objectValue.email);

You should get something like this:

[ { email: '[email protected]'} ]

If you need _id, use this in select {email: 1, _id: 1}

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

This is a simple JavaScript:

var res = [ 
   { _id: '5a9f8849fc49ca1ff4aee3dc',
    email: '[email protected]',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' },
   { _id: '5a9f8849fc49ca1ff4aee3dd',
    email: '[email protected]',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' },
   { _id: '5a9f8849fc49ca1ff4aee3de',
    email: '[email protected]',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' }
];

var finalRes = res.map(({email}) => ({email}));
console.log(finalRes);

Upvotes: 1

h1b9b
h1b9b

Reputation: 1070

You can use reduce or map on your array:

Using reduce

reducedResults = result.reduce((accumulator, current) => {
   accumulator.push({ email: current.email });
   return accumulator;
}, [])

Using map

mappedResults = result.map((user) => {
   return { email: user.email };
})

Upvotes: 0

Related Questions