By the same logic, in my case, I want the output as JSON data into out.json
1- Create file.js on local, not on the server
date = new Date("2019-09-01");
query = {x: true, 'created': {$gte: date}};
now = new Date();
userQuery = {'user.email': {$nin: [/\.dev$|@test\.com$|@testing\.com$/]}};
data = db.companies.aggregate([
{$match: query},
{$sort: {x: 1, created: 1}},
{$lookup: {as: 'user', from: 'users', localField: 'creator', foreignField: '_id'}},
{$addFields: {user: {$arrayElemAt: ['$user', 0]}}},
{$match: userQuery},
{$project: {"_id": 1, "name": 1, "user.email": 1, "xyz": 1}}
]).toArray();
print(JSON.stringify(data));
2- Run
mongo server/collection file.js > out.json --quiet
3- Delete header and check out.json
[
{
"_id": "124564789",
"xyz": [
{
"name": "x",
"value": "1"
},
{
"name": "y",
"value": "2"
}
],
"name": "LOL",
"user": {
"email": "[email protected]"
}
},
....
....
}]
mongoexport
has--query
option, but it doesn't support complex queries like yours. – Boyd