Retrieve only particular HashMap entry ( Projection )

Faraz Ka

Ranch Hand

Posts: 80

posted 1 year ago

Hello,

I am new to MongoDB. In my document there is one HashMap entry ( projection)
I want to write a query so that it returns me ONLY the HashMap entry ( projection) from a particular document and not the whole document so that I can manipulate the HashMap object in my Java code.

Not sure what you mean here - MongoDB stores JSON-style documents, which are not exactly the same as hashmaps.

The MongoDB online manuals contain lots of good advice on how to do different kinds of queries via the MongoDB shell. This might be a good place to start as you can try out your queries directly on the database. It sounds like you simply want to limit the output to specified fields, which is a "projection".

If you are using another language e.g. Java or Python, then the relevant database driver documentation should provide examples of how to construct your query document to achieve your requirements e.g. the MongoDB Java driver documentation includes a quick introduction.

But if I were you I'd start in the shell, so you can concentrate on getting your query right first. Once you know what your query should look like, it will be easier to build the corresponding query in Java or Python. This is a good general approach to building database queries, even on SQL databases, as it means you can focus on getting the query right, before you have to worry about how to make it work via your driver.