<tfoot id='74ohW'></tfoot>
  • <small id='74ohW'></small><noframes id='74ohW'>

    <legend id='74ohW'><style id='74ohW'><dir id='74ohW'><q id='74ohW'></q></dir></style></legend>
        <bdo id='74ohW'></bdo><ul id='74ohW'></ul>
        <i id='74ohW'><tr id='74ohW'><dt id='74ohW'><q id='74ohW'><span id='74ohW'><b id='74ohW'><form id='74ohW'><ins id='74ohW'></ins><ul id='74ohW'></ul><sub id='74ohW'></sub></form><legend id='74ohW'></legend><bdo id='74ohW'><pre id='74ohW'><center id='74ohW'></center></pre></bdo></b><th id='74ohW'></th></span></q></dt></tr></i><div id='74ohW'><tfoot id='74ohW'></tfoot><dl id='74ohW'><fieldset id='74ohW'></fieldset></dl></div>

        在 mongodb 集合中查找一些值?

        时间:2023-09-28

          <tfoot id='sOykh'></tfoot>
          <i id='sOykh'><tr id='sOykh'><dt id='sOykh'><q id='sOykh'><span id='sOykh'><b id='sOykh'><form id='sOykh'><ins id='sOykh'></ins><ul id='sOykh'></ul><sub id='sOykh'></sub></form><legend id='sOykh'></legend><bdo id='sOykh'><pre id='sOykh'><center id='sOykh'></center></pre></bdo></b><th id='sOykh'></th></span></q></dt></tr></i><div id='sOykh'><tfoot id='sOykh'></tfoot><dl id='sOykh'><fieldset id='sOykh'></fieldset></dl></div>

            <bdo id='sOykh'></bdo><ul id='sOykh'></ul>
            • <legend id='sOykh'><style id='sOykh'><dir id='sOykh'><q id='sOykh'></q></dir></style></legend>
                <tbody id='sOykh'></tbody>

                  <small id='sOykh'></small><noframes id='sOykh'>

                  本文介绍了在 mongodb 集合中查找一些值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  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

                  解决方案

                  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.

                  这篇关于在 mongodb 集合中查找一些值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 mongodb 中读取数组? 下一篇:如何返回 N1qlQueryResult 作为 Couchbase 数据库的 RE

                  相关文章

                  最新文章

                  <legend id='B9pgB'><style id='B9pgB'><dir id='B9pgB'><q id='B9pgB'></q></dir></style></legend>

                    <small id='B9pgB'></small><noframes id='B9pgB'>

                      <bdo id='B9pgB'></bdo><ul id='B9pgB'></ul>
                  1. <tfoot id='B9pgB'></tfoot>

                  2. <i id='B9pgB'><tr id='B9pgB'><dt id='B9pgB'><q id='B9pgB'><span id='B9pgB'><b id='B9pgB'><form id='B9pgB'><ins id='B9pgB'></ins><ul id='B9pgB'></ul><sub id='B9pgB'></sub></form><legend id='B9pgB'></legend><bdo id='B9pgB'><pre id='B9pgB'><center id='B9pgB'></center></pre></bdo></b><th id='B9pgB'></th></span></q></dt></tr></i><div id='B9pgB'><tfoot id='B9pgB'></tfoot><dl id='B9pgB'><fieldset id='B9pgB'></fieldset></dl></div>