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

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

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

        在 mongodb 中读取数组?

        时间:2023-09-28
          <tbody id='lWsPj'></tbody>

          • <bdo id='lWsPj'></bdo><ul id='lWsPj'></ul>
            • <tfoot id='lWsPj'></tfoot>

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

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

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

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

                  问题描述

                  您好,我在 mongodb 中遇到了一些问题.用 java 读取文档是没有问题的,但是读取文档中的数组是一个问题.假设我有一个集合 myCol:

                  Hi I have some trouble with arrays in mongodb. To read a document with java is no problem but to read an array what is in a document is a problem. Lets say I have a collection myCol:

                  {"name": "lenny linux", "gender": "m", "computers": [{"name": "computer"}, {"name": "computer2"} {"name"...}]}
                  

                  所以有一个带有计算机的阵列.我可以阅读整个文档

                  So there is an array with computers. I could read the whole document with

                          DBCollection myCol = getCollection(...);
                          BasicDBObject query = new BasicDBObject();
                          query.put(name, "lenny linux");
                  
                          DBCursor cursor = myCol.find(query);
                          while (cursor.hasNext()) {
                              System.out.print(cursor.next());
                          }
                  

                  但我只需要计算机的名称,所以我必须以某种方式读取数组.不要在 mongodb 中获取这个数组的东西.如果我想从 mongodb 数组中删除一些东西怎么办?它与删除普通文档不同...谢谢您的帮助!

                  But I just need the names of the computers, so I have to read somehow the array. Dont get this array stuff in mongodb. And also what if I would like to delete something from a mongodb array? Its not the same as to delete a normal document... thank you for any help!

                  如果我正在阅读 mongodb 页面:http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray 我真的不明白.他们有一系列颜色,然后他们像这样读取红色:

                  If im reading the mongodb page: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray I really dont get it. They have there an array of colors and then they are reading red like this:

                  db.things.find({ colors :"red" });
                  

                  我为什么要这样做?如果我想读取数组以了解数组内部的内容.用户不知道有红色"或蓝色或其他.也许数组颜色是空的?然后我返回一个空值、0 或其他任何值,如果有 4 种颜色,那么给我这些颜色,打印出来.我没有任何其他的例子......我很抱歉我的英语不好.

                  Why would I do this? If I want to read an array to know whats inside the array. The user dont know that there is a "red" or blue or whatever. Maybe the array colors is empty? Then I get back a null, 0 or whatever and if there are 4 colors then give me these colors, print it out. I dont have any other examples...im sorry for my bad english.

                  好的,所以对我来说新的解决方案是获取 name == lenny linux 的整个文档(就像我的代码中的第一次一样),然后使用像 json-simple.好吧,也许这不是最好的解决方案,因为最好的解决方案是在没有其他库的情况下仅使用 mongolib 来获取数组中的东西......但是它可以工作:) 如果有人知道更简单的方法,只需将其发布在这里.谢谢.

                  Ok so the new solution for me is to get the whole document where name == lenny linux (like at the first time in my code) and then to parse this document with an extern JSON parser like json-simple. Well maybe thats not the best solution, because the best solution would be to get the stuff in the array without other libs just using the mongolib... but ok its working :) If somebody knows an easier way just post it here. Thank you.

                  推荐答案

                  当使用对象(命名元素)进入数组时,您希望使用点表示法进入数组.

                  When reaching into an array with objects (named elements), you want to use the dot notation to reach into the array.

                  db.myColl.find({'computers.name':'computer'});

                  http://www.mongodb.org/display/DOCS/高级+查询#AdvancedQueries-ValueinanArray

                  至于从数组中删除项目,你想看看 $pop$pull 更新函数

                  as for removing items from an array, you want to look at the $pop and $pull update functions

                  http://www.mongodb.org/display/DOCS/Updating

                  这篇关于在 mongodb 中读取数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:哪个嵌入式数据库用 Java 编写用于简单的键/值存 下一篇:在 mongodb 集合中查找一些值?

                  相关文章

                  最新文章

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

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

                    <tfoot id='KaivV'></tfoot>
                    <legend id='KaivV'><style id='KaivV'><dir id='KaivV'><q id='KaivV'></q></dir></style></legend>