Retrieve all the keys of an object inside a mongoose document
Try somthing as below:
db.collection.aggregate([ { $project: { books: { $objectToArray: "$books" } } }, { $unwind: "$books" }, { $group: { _id: null, books: { $push: "$books.k" } } }])
The result will be as below:
{ "_id" : null, "books" : [ "Harry Potter", "Hunger Games", "The Great Gatsby", "Frankenstein" ]}