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

  • SEARCH
  • Home
  • 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 6874711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:13:44+00:00 2026-05-27T04:13:44+00:00

Im trying to read a (mongo)userdatabase with java. On the tutorial page I saw

  • 0

Im trying to read a (mongo)userdatabase with java. On the tutorial page I saw how to read the whole collection. I can do something like that:

    DBCursor cursor = col.find();
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }

Now if I have a collection with users := name, age, password (…) and whatever. Now I would like to find a name with a password. For example for a login process. Lets say I have two strings: String n and p. If there is a user.equals(n) and a password.equals(p) in the database then login user. How do I have to change my cursor? I saw some query examples on the mongodb java tutorial page, but duh I really dont get it…

Any ideas? Thank you

  • 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-05-27T04:13:44+00:00Added an answer on May 27, 2026 at 4:13 am

    Awesome, you’ll love Mongo.

    In the example you posted, the program iterates through a set of results. In the user/password problem you describe what you are actually trying to do is get one document (not a set of documents) based on some criteria.

    On the shell that would look like this:

    n = "login"
    p = "password"
    
    db.users.findOne({ user: n, password: p})
    

    Notice I’m using findOne instead of find which returns a document instead of a cursor to many documents.

    Now, lets take a look at the java driver’s example:

    BasicDBObject query = new BasicDBObject();
    
    query.put("i", 71);
    cur = coll.find(query);
    
    while(cur.hasNext()) {
        System.out.println(cur.next());
    }
    

    The BasicDBObject creates the query object and then you put different criteria which together form your query.

    So instead of query.put("i", 71); you would do something like:

    query.put("user", n)
    query.put("password", p)
    

    and… instead of the while loop just use findOne instead of find so you don’t have to iterate over the result set of 1 object (pointless).

    You can read more about findOne() here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a .doc file into a database so that I can
im trying to read newline terminated strings using windows api and saw that the
I am trying to read in from a few files using something like this
Trying to read an RSS and select information using Linq but can't seem to
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
I'm trying to optimize a concurrent collection that tries to minimize lock contention for
Trying to read into an object the following XML file (can be changed) into
Hey trying to read stream into image control can any one help also I
I've got a collection of short WAV files that I would like to process
I'm trying to read from a text file to input data to my java

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.