Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:16:12+00:00 2026-06-01T12:16:12+00:00

Suppose I want to query for only a certain attribute of all documents, something

  • 0

Suppose I want to query for only a certain attribute of all documents, something like in SQL:

SELECT FIRSTNAME
FROM TABLE1

How can I do it with Mongo and it’s Java API?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:16:14+00:00Added an answer on June 1, 2026 at 12:16 pm

    In the Java API, you can do it like this. You have to explicitly turn off the _id field, in order to exclude it.

    Mongo m = new Mongo();
    DB db = m.getDB( "test" );
    
    DBCollection coll = db.getCollection("test");       
    coll.insert(new BasicDBObject("Name","Wes").append("x", "to have a second field"));
    BasicDBObject query = new BasicDBObject(); 
    BasicDBObject fields = new BasicDBObject("Name",1).append("_id",false);
    DBCursor curs = coll.find(query, fields);
    
    while(curs.hasNext()) {
      DBObject o = curs.next();
      System.out.println(o.toString());
    }
    

    Output:

    { "Name" : "Wes"}
    

    Update for sort:

    coll.insert(new BasicDBObject("Name","Wes").append("x", "to have a second field"));
    coll.insert(new BasicDBObject("Name","Alex").append("x", "to have a second field"));
    coll.insert(new BasicDBObject("Name","Zeus").append("x", "to have a second field"));
    BasicDBObject query = new BasicDBObject(); 
    BasicDBObject fields = new BasicDBObject("Name",1).append("_id",false);
    BasicDBObject orderBy = new BasicDBObject("Name",1);
    DBCursor curs = coll.find(query, fields).sort(orderBy);
    while(curs.hasNext()) {
       DBObject o = curs.next();
       System.out.println(o.toString());
    }
    

    Gives:

    { "Name" : "Alex"}
    { "Name" : "Wes"}
    { "Name" : "Zeus"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to select some data from db and store in an array. Suppose
Suppose I want to implement a curses/console like program in HTML/CSS/Javascript. Examples might be
Suppose I want to count the lines of code in a project. If all
In SQL Server 2008 I would like to create a stored procedure to select
How do you do SQL query for the following condition? Suppose you have two
suppose i have a query like this : $std_id = $_POST['std_id']; $name = $_POST['name'];
I have one query in SQL Server output, Suppose i have one table (Ex.StudentMaster)
I am usung oracle 11g. Suppose the following query returns n rows. SELECT t.id,t.from_date,t.price
What's the best way retrieve complex queries from a REST service? Suppose I want
Suppose I have the two following Linq-To-SQL queries I want to refactor: var someValue1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.