Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

mongodb - MongodDB with Java driver: How to find nested atributes and how to use "and" operator

The collection:

{"_id":{"$oid":"5c7d656a82bb44be9e770a7e"},"name":"Harry Potter","species":"human","gender":"male","house":"Gryffindor","dateOfBirth":"31-07-1980","yearOfBirth":{"$numberInt":"1980"},"ancestry":"half-blood","eyeColour":"green","hairColour":"black","wand":{"wood":"holly","core":"phoenix feather","length":{"$numberInt":"11"}},"patronus":"stag","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Daniel Radcliffe","alive":true,"image":"http://hp-api.herokuapp.com/images/harry.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a7f"},"name":"Hermione Granger","species":"human","gender":"female","house":"Gryffindor","dateOfBirth":"19-09-1979","yearOfBirth":{"$numberInt":"1979"},"ancestry":"muggleborn","eyeColour":"brown","hairColour":"brown","wand":{"wood":"vine","core":"dragon heartstring","length":""},"patronus":"otter","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Emma Watson","alive":true,"image":"http://hp-api.herokuapp.com/images/hermione.jpeg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a80"},"name":"Ron Weasley","species":"human","gender":"male","house":"Gryffindor","dateOfBirth":"01-03-1980","yearOfBirth":{"$numberInt":"1980"},"ancestry":"pure-blood","eyeColour":"blue","hairColour":"red","wand":{"wood":"willow","core":"unicorn tail-hair","length":{"$numberInt":"14"}},"patronus":"Jack Russell terrier","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Rupert Grint","alive":true,"image":"http://hp-api.herokuapp.com/images/ron.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a81"},"name":"Draco Malfoy","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"05-06-1980","yearOfBirth":{"$numberInt":"1980"},"ancestry":"pure-blood","eyeColour":"grey","hairColour":"blonde","wand":{"wood":"hawthorn","core":"unicorn tail-hair","length":{"$numberInt":"10"}},"patronus":"","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Tom Felton","alive":true,"image":"http://hp-api.herokuapp.com/images/draco.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a82"},"name":"Minerva McGonagall","species":"human","gender":"female","house":"Gryffindor","dateOfBirth":"04-10-1925","yearOfBirth":{"$numberInt":"1925"},"ancestry":"","eyeColour":"","hairColour":"black","wand":{"wood":"","core":"","length":""},"patronus":"tabby cat","hogwartsStudent":false,"hogwartsStaff":true,"actor":"Dame Maggie Smith","alive":true,"image":"http://hp-api.herokuapp.com/images/mcgonagall.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a83"},"name":"Cedric Diggory","species":"human","gender":"male","house":"Hufflepuff","dateOfBirth":"","yearOfBirth":{"$numberInt":"1977"},"ancestry":"","eyeColour":"grey","hairColour":"brown","wand":{"wood":"ash","core":"unicorn hair","length":{"$numberDouble":"12.25"}},"patronus":"","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Robert Pattinson","alive":false,"image":"http://hp-api.herokuapp.com/images/cedric.png"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a84"},"name":"Cho Chang","species":"human","gender":"female","house":"Ravenclaw","dateOfBirth":"","yearOfBirth":"","ancestry":"","eyeColour":"brown","hairColour":"black","wand":{"wood":"","core":"","length":""},"patronus":"swan","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Katie Leung","alive":true,"image":"http://hp-api.herokuapp.com/images/cho.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a85"},"name":"Severus Snape","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"09-01-1960","yearOfBirth":{"$numberInt":"1960"},"ancestry":"half-blood","eyeColour":"black","hairColour":"black","wand":{"wood":"","core":"","length":""},"patronus":"doe","hogwartsStudent":false,"hogwartsStaff":true,"actor":"Alan Rickman","alive":false,"image":"http://hp-api.herokuapp.com/images/snape.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a86"},"name":"Rubeus Hagrid","species":"half-giant","gender":"male","house":"Gryffindor","dateOfBirth":"06-12-1928","yearOfBirth":{"$numberInt":"1928"},"ancestry":"half-blood","eyeColour":"black","hairColour":"black","wand":{"wood":"oak","core":"","length":{"$numberInt":"16"}},"patronus":"","hogwartsStudent":false,"hogwartsStaff":true,"actor":"Robbie Coltrane","alive":true,"image":"http://hp-api.herokuapp.com/images/hagrid.png"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a87"},"name":"Neville Longbottom","species":"human","gender":"male","house":"Gryffindor","dateOfBirth":"30-07-1980","yearOfBirth":{"$numberInt":"1980"},"ancestry":"pure-blood","eyeColour":"","hairColour":"blonde","wand":{"wood":"cherry","core":"unicorn tail-hair","length":{"$numberInt":"13"}},"patronus":"","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Matthew Lewis","alive":true,"image":"http://hp-api.herokuapp.com/images/neville.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a88"},"name":"Luna Lovegood","species":"human","gender":"female","house":"Ravenclaw","dateOfBirth":"13-02-1981","yearOfBirth":{"$numberInt":"1981"},"ancestry":"","eyeColour":"grey","hairColour":"blonde","wand":{"wood":"","core":"","length":""},"patronus":"hare","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Evanna Lynch","alive":true,"image":"http://hp-api.herokuapp.com/images/luna.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a89"},"name":"Ginny Weasley","species":"human","gender":"female","house":"Gryffindor","dateOfBirth":"11-08-1981","yearOfBirth":{"$numberInt":"1981"},"ancestry":"pure-blood","eyeColour":"brown","hairColour":"red","wand":{"wood":"yew","core":"","length":""},"patronus":"horse","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Bonnie Wright","alive":true,"image":"http://hp-api.herokuapp.com/images/ginny.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a8a"},"name":"Sirius Black","species":"human","gender":"male","house":"Gryffindor","dateOfBirth":"03-11-1959","yearOfBirth":{"$numberInt":"1959"},"ancestry":"pure-blood","eyeColour":"grey","hairColour":"black","wand":{"wood":"","core":"","length":""},"patronus":"hare","hogwartsStudent":false,"hogwartsStaff":false,"actor":"Gary Oldman","alive":false,"image":"http://hp-api.herokuapp.com/images/sirius.JPG"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a8b"},"name":"Remus Lupin","species":"werewolf","gender":"male","house":"Gryffindor","dateOfBirth":"10-03-1960","yearOfBirth":{"$numberInt":"1960"},"ancestry":"half-blood","eyeColour":"green","hairColour":"brown","wand":{"wood":"cypress","core":"unicorn tail-hair","length":{"$numberDouble":"10.25"}},"patronus":"wolf","hogwartsStudent":false,"hogwartsStaff":true,"actor":"David Thewlis","alive":false,"image":"http://hp-api.herokuapp.com/images/lupin.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a8c"},&qu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You were not sufficiently clear about what was the result of your approaches. Do they give execution errors or don't they bring the results you expected?

For the first problem, it seems that alive and hogwartsStudents are of boolean type, so I recommend to use true instead of "true"

FindIterable<Document> aliveStudents = harrypotter.find(and(eq("alive", true), eq("hogwartsStudent", true)));
System.out.println("---------- ALIVE STUDENTS ----------");
for (Object character : aliveStudents) {System.out.println(((Document) character).toJson());}

For the second problem, I think you should use the fieldName.fieldProperty syntax:

FindIterable<Document> wand= harrypotter.find(eq("wand.wood", "holly"));
System.out.println("---------- CHARACTERS WITH HOLLY WAND ----------");
for (Object character: wand) {System.out.println(((Document) character).toJson());}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...