您好,我在 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模板网!