1

I want to add array in getting response in mongodb response (data fetching from mongo)

    { _id: 23232385,
      CompanyId: 232385,
      CompanyCd: 'MONDLAY',
      CompanyName: 'Company Name ',
      Competition: false,
      ParentCompany: 707,
      CareOf: '',
      AddressLine2: '',
      AddressLine3: '',
      Suite: '',
      City: 'Los Angeles',
      StateCd: 'CA',
      Zip: '90010',
      Title: '',
      Phone: '549-334',
      Extension: '',
      Fax:  '549-9834',
      Comment: '',
      Abrev: '',
      Email: '[email protected]' }

And want this response in JSON below like format.

[      
        { _id: 23232385,
          CompanyId: 232385,
          CompanyCd: 'MONDLAY',
          CompanyName: 'Company Name ',
          Competition: false,
          ParentCompany: 707,
          CareOf: '',
          AddressLine2: '',
          AddressLine3: '',
          Suite: '',
          City: 'Los Angeles',
          StateCd: 'CA',
          Zip: '90010',
          Title: '',
          Phone: '549-334',
          Extension: '',
          Fax:  '549-9834',
          Comment: '',
          Abrev: '',
          Email: '[email protected]' }
]

Also want something which goes with function. Also trying to implement the same thing with huge data set. Can anyone help me with this?

Thanks in advance

2
  • 1
    can you post your query also. Commented Sep 23, 2019 at 12:56
  • collection.find().each(function(err, item, response, status) { console.log(item) Array.from(item).forEach(itemdata => { bulk.push({index:{ _index: esIndexName, _type: mongoCollectionName, } }) bulk.push(itemdata) }) here I'm trying to convert item adding object array @SaurabhAgrawal Commented Sep 23, 2019 at 12:59

1 Answer 1

1

You can use cursor.toArray() from mongoDB.

Try like this example:

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("url", function (_err, db) {
  var dbo = db.db("myDB");
  dbo.collection("myCollection").find({}).toArray(function (err, result) {
    console.log(result);//this will be array
  })
})
Sign up to request clarification or add additional context in comments.

3 Comments

Can you edit the code I shared? Seems like I'm doing some mistake, data is getting lost.
Everything seems to be fine with your question.

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.