I have a collection including very large nested document. When I run a query like db.xxx.find()
or db.xxx.findOne()
, it will return a huge amount of data. Is there a way for me to suppress the returned document? I want to find the first level fields and ignore the nested document.
I know I can use projects
to show the fields I am interested. But my case is that I don't know anything about the document before I run the query command. I am creating a mongodb tool to help others to query on their collection. So I am looking for a general solution to query the first level fields and ignore nested document.
Asked
Active
Viewed 277 times
0

Joey Yi Zhao
- 37,514
- 71
- 268
- 523
-
Also see [Project Fields to Return from Query](https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/) in the core documentation. Not including the "array" is as simple as `db.xxx.find({},{ "arrayField": 0 })` where the `0` means *"do not return this field"* – Neil Lunn Jul 06 '17 at 01:04
-
I have updated my question to make it more clear. Actually I am looking for a general solution. I don't know the document fields name before running query command. – Joey Yi Zhao Jul 06 '17 at 02:44
-
Then you are in a whole lot of trouble. If it's an "unknown key" then any operation would need to inspect every single document and then somehow "magically" work out that you did not want to display such a field. Aside from basic "exclusion" the only thing capable of "manipulating a document" would be using the aggregation framework, and then only in most recent releases, **AND THEN** it's completely horrible since you need scan the whole collection in order to actually manipulate it. – Neil Lunn Jul 06 '17 at 02:49
-
So your "revision" is still really unclear, and the problem is not tenable anyway.You need to change your schema to something more practical that works within the constraints. You've been linked to "what MongoDB **CAN** do". I suggest you [learn from the example and work with what is actually feasible](https://stackoverflow.com/questions/25589113/how-to-select-a-single-field-in-mongodb). – Neil Lunn Jul 06 '17 at 02:50