Hello, I want to extract all the fields present in a collection, including nested fields. How can I extract the fields using shell or compass?
Skip to main content
In mongosh
, is this what you want?
db.mycollection.aggregate([ { $group: { '_id': "$_id", 'otherFields': { $push: { fields: "$$ROOT" } } } }] )
See also https://www.mongodb.com/docs/manual/reference/operator/aggregation/group
Hello Jack,
I want to list out only field names present in a collection. values are not required.
Object.keys(db.YOURCOLLECTION.findOne())
gets the top level.
You’d need to write something recursive like that to get the sub fields.
New & Unread Topics
Topic | Replies | Views | Activity |
---|---|---|---|
Error “Invalid UTF-8 string in BSON document” after rebuild on Windows | 0 | 152 | Aug 2024 |
How to build the static library of a specific MongoDB server module | 0 | 49 | Aug 2024 |
Want to know more about mongod OS user | 2 | 68 | Sep 2024 |
5.0.29 release date? | 0 | 37 | Sep 2024 |
mongo build without SSE4 | 0 | 26 | Feb 12 |