在mongodb中读取数组? [英] Reading an array in mongodb?

查看:102
本文介绍了在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.

编辑2:
好​​的,所以新的解决方案我是把整个文件放在name == lenny linux的地方(就像我第一次在我的代码中),然后用一个extern JSON解析器来解析这个文件,比如 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/Advanced+Queries#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中读取数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆