问题
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());
}
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!
Edit: 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" });
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.
Edit2: 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.
回答1:
And also what if I would like to delete something from a mongodb array? Its not the same as to delete a normal document.
http://www.mongodb.org/display/DOCS/Updating explains 2 ways:
$set: to replace the current array with a new one (fetch the previous array, remove an element or two, and update with $set)
db.users.update({name : "lenny linux"}, {$pull : { computers : { name : "computer2" } }}, false, false)
to remove all elements from the arraycomputers
that havename
'computer2'.
回答2:
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
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
回答3:
Since I was dealing with this problem in Java, and your code is in Java, here is the solution ... in java.
Basically when you get(key)
a MongoDB array, it returns a com.mongodb.BasicDBList
object that you can get an iterator for.
In my code, I query documents that look like this:
{
"_id": ObjectID("52d60db91f3aa664410bcde5"),
"owner": "Amy",
"trusted": [
"amy",
"paul",
"randy"
]
}
And when I ever need to find the trusted people of this document, I use ((BasicDBList) doc.get("trusted")).listIterator();
DBObject doc = collection.findOne(new BasicDBObject("owner", "Amy"))
ListIterator<Object> trustedList = ((BasicDBList) doc.get("trusted")).listIterator();
while(trustedList.hasNext()){
Object nextItem = trustedList.next();
System.out.println(nextItem.getClass()); // String
// Here I handle what to do to each name.
// I could add them to an ArrayList<String> if I needed
updateTrustee((String)nextItem);
}
I came to this solution using a few System.out.println(Object.getClass())
s to know what classes I needed to cast.
回答4:
The question he is asking is how does one return ALL the elements found in an array stored in a Mongo collection without knowing precisely how many or what these elements might be. You are not performing a query against a known value, rather you are simply asking for a dump of what is in the Mongo array. For example, if your Mongo array name is "colors", simply do the following:
@colors = @{$record->{colors}};
Now you have all the colors in a Perl array for you to play with.
Enjoy!
来源:https://stackoverflow.com/questions/8957900/reading-an-array-in-mongodb