0

I have customer scheema in which I am storing customer details with referenceId of address(it is array of addresses for eg one customer can have many addresses). And I have multiple fields like street, area, landmark in address schema. I have to find all customers who's area is viman nagar, kalyani nagar or KP. Further I have to export this result into csv file.

I know one traditional method for doing this is, find all customers, 'populate' their addresses and finally loop through result and modify result with only customers whose address contain viman nagar, kalyani nagar or KP as 'area'.

And for export result into CSV I'll use milan's answer from here.

Can I get customer list in single query without looping through result and modifying result set? or suggest me any good option to achieve this.

var getCustomerToExport = function(req, res) {
  var filteredResult = [];
Customer.find().populate('address').exec(function(err, customers){
    async.forEach(customers, function(c) {
       var newCustomer= c.address.filter(function(a, indx, arr){ return(a.area === 'viman nagar' || a.area === 'kalyani nagar' || a.area === 'KP'); });
       if(newCustomer.length > 0)
       filteredResult.push(c);
    });
    res.json(filteredResult);
  });
};
Community
  • 1
  • 1
zulekha
  • 313
  • 9
  • 17
  • 1
    Where do you propose to run this from? You use terms like "populate" which implies you are using nodejs and mongoose. Then you link to an answer about outputing data from the mongodb shell. Your question also gives no concept of the structure or relation of the data you wish to "populate". In all honesty, this is both pretty broad and pretty vague, with an overall air of "please someone write this for me". Please share what you have done so far. – Blakes Seven Aug 26 '15 at 09:16
  • Please use the edit link on your question to include relevant details rather than postings links or details in comments. There, I have done that for you. This does not however show any detail about what the data result looks like or how the expected CSV output is supposed to come out. You are probably better off researching CSV libraries for node as I see nothing being used here. Also the `async.forEach` seems to refer to some function I no nothing of. There is a [`asycn.forEachOf`](https://github.com/caolan/async#forEachOf), but it's usage is totally different to what you are doing. – Blakes Seven Aug 26 '15 at 11:12
  • I haven't done csv part yet, I am getting expected result with this part of code but my question is that, Can i get this filtered data without using for loop? – zulekha Aug 26 '15 at 11:21
  • What I would like you to do here is break down your question. My point is there is no attempt at writing a CSV here, so if you are asking "how do I do that" then the question is "too broad" or "opinion based" or "asking for tools" as just a few of the reasons why your question would be closed. If you can make your question about something "specific", then that is fine. But right now you still basically in a start from scratch mode. – Blakes Seven Aug 26 '15 at 11:30

0 Answers0