<commit_before>var mongoDB = require('mongodb');
var Promise = require('bluebird');

var findOne= require('./findOne');

module.exports= function (modelName,model,obj,fields) {

	var models= require('../../utils/getModels');
	var ModelReference=[];

	if(model.hasOwnProperty('reference'))
	{
		for (var key in model.reference)
		{
			if (obj.hasOwnProperty(key)) {
				var keyvalue={};
				keyvalue.key=key;
				keyvalue.value=model.reference[key].model;
				ModelReference.push(keyvalue);
			};
		}
	}

	// console.log(ModelReference,fields);
	
	if(fields && fields.length > 0)
	{
		ModelReference=ModelReference.filter(function (val) {
			if(fields.indexOf(val.key) >=0)
			{
				return val;
			}
		});
	}
	
	// console.log(ModelReference);

	var PromiseArr=[];	


	ModelReference.map(function (val) {
		if(models[val.value].hasOwnProperty('embeded') )
		{
			PromiseArr.push(findOne(val.value,obj[val.key],models[val.value].embeded));
		}
		else
		{
			PromiseArr.push(findOne(val.value,obj[val.key]));
		} 
	});

	return Promise.all(PromiseArr)
	.then(function (res) {
		ModelReference.map(function (val,index) {
			obj[val.key]=res[index];
		});
		// console.log(obj);
		return obj;
	});<commit_msg>Patched embeded find for populate<commit_after>var mongoDB = require('mongodb');
var Promise = require('bluebird');

var findOne= require('./findOne');
var findOneEmbeded= require('./findOneEmbeded');

module.exports= function (modelName,model,obj,fields) {

	var models= require('../../utils/getModels');
	var ModelReference=[];

	if(model.hasOwnProperty('reference'))
	{
		for (var key in model.reference)
		{
			if (obj.hasOwnProperty(key)) {
				var keyvalue={};
				keyvalue.key=key;
				keyvalue.value=model.reference[key].model;
				ModelReference.push(keyvalue);
			};
		}
	}

	// console.log(ModelReference,fields);
	
	if(fields && fields.length > 0)
	{
		ModelReference=ModelReference.filter(function (val) {
			if(fields.indexOf(val.key) >=0)
			{
				return val;
			}
		});
	}
	
	// console.log(ModelReference);

	var PromiseArr=[];	


	ModelReference.map(function (val) {
		if(models[val.value].hasOwnProperty('embeded') )
		{
			PromiseArr.push(findOneEmbeded(val.value,obj[val.key],models[val.value].embeded));
		}
		else
		{
			PromiseArr.push(findOne(val.value,obj[val.key]));
		} 
	});

	return Promise.all(PromiseArr)
	.then(function (res) {
		ModelReference.map(function (val,index) {
			obj[val.key]=res[index];
		});
		// console.log(obj);
		return obj;
	});
{'fraction_non_alphanumeric': 0.17713365539452497, 'fraction_numerical': 0.001610305958132045, 'mean_word_length': 4.071428571428571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3490465', 'n_tokens_mistral': 987, 'n_tokens_neox': 868, 'n_words': 125}