Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Retrieve only particular HashMap entry ( Projection )

Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


Posts: 2407
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I would also recommend you complete one of the free online courses from MongoDB University e.g. M101J MongoDB for Java Developers and M101P MongoDB for Python Developers are both starting again from today. These courses will teach you all the basics about how to work with MongoDB in your chosen language, and they're a great way to get up to speed quickly with this technology.
Faraz Ka
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris!
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic