agmission/Others/scripts/c_users_map.js

52 lines
1.3 KiB
JavaScript

// Aggregate from user to user's settings
var custUsers = db.customers.aggregate([
{ $match: { active: true } },
// { $project: { lang: { $ifNull: [ "$lang", "en" ] } } },
{
$lookup:
{
from: "users",
localField: "user",
foreignField: "_id",
as: "c_users"
}
},
{ $unwind: { path: "$c_users", "preserveNullAndEmptyArrays": true } },
{
$project: {
'_id': 0,
'id': '$c_users._id',
'name': 1,
// 'contact': 1,
'username':'$c_users.username'
}
}
]).toArray();
// print (custUsers)
var phead = true, hline = '', line = '';
custUsers.forEach(cu => {
for (var p in cu) {
if (phead) {
if (p == 'id')
hline = 'userId,' + hline;
else
hline += p + ','
}
if (p == 'id')
line = cu[p] + ',' + line;
else {
// if (cu[p].indexOf(',') != -1)
// print (cu[p])
cu[p] = (cu[p] || '').replace(/,/g, '');
line = line + cu[p] + ',';
}
}
if (phead) {
print (hline.slice(0, -1))
phead = false;
}
print (line.slice(0, -1))
line = '';
});