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 |
---|---|---|---|
Support for windows-arm64 | 3 | 1.2k | 14d |
Using OpenAI Latest Embeddings In A RAG System With MongoDB community Edition | 1 | 480 | May 2024 |
applyOps: (KeyNotFound) No keys found for HMAC that is valid for time | 1 | 599 | Jul 2024 |
Best Practices for BulkWrite Performance with Large Documents and Array Fields in a 2-Node Replica Set | 1 | 514 | Sep 2024 |
MongoDB with “tcmallocReleaseRate”: 3, Tracking Operations in Logs | 0 | 26 | Oct 2024 |