// export_shared_agents.js // Emits strict Extended JSON to stdout (safe JSON for ObjectId/Date) const pipeline = [ { $match: { resourceType: "agent" } }, { $group: { _id: "$resourceId", count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }, { $lookup: { from: "agents", localField: "_id", foreignField: "_id", as: "agentDetails" } }, { $unwind: "$agentDetails" }, { $lookup: { from: "users", localField: "agentDetails.author", foreignField: "_id", as: "authorDetails" } }, { $unwind: { path: "$authorDetails", preserveNullAndEmptyArrays: true } }, { $set: { "agentDetails.author": "$authorDetails.email" } }, { $project: { "agentDetails.versions": 0, "agentDetails.instructions": 0, "authorDetails": 0 } } ]; const out = db.aclentries.aggregate(pipeline).toArray(); print(EJSON.stringify(out, { relaxed: false, indent: 2 }));