簡體   English   中英

帶有 Java 驅動程序的 MongodDB:如何查找嵌套屬性以及如何使用“和”運算符

[英]MongodDB with Java driver: How to find nested atributes and how to use “and” operator

收藏:

{"_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"},"name":"Arthur Weasley","species":"human","gender":"male","house":"Gryffindor","dateOfBirth":"06-02-1950","yearOfBirth":{"$numberInt":"1950"},"ancestry":"pure-blood","eyeColour":"blue","hairColour":"red","wand":{"wood":"","core":"","length":""},"patronus":"weasel","hogwartsStudent":false,"hogwartsStaff":false,"actor":"Mark Williams","alive":true,"image":"http://hp-api.herokuapp.com/images/arthur.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a8d"},"name":"Bellatrix Lestrange","species":"human","gender":"female","house":"Slytherin","dateOfBirth":"","yearOfBirth":{"$numberInt":"1951"},"ancestry":"pure-blood","eyeColour":"brown","hairColour":"black","wand":{"wood":"walnut","core":"dragon heartstring","length":{"$numberDouble":"12.75"}},"patronus":"","hogwartsStudent":false,"hogwartsStaff":false,"actor":"Helena Bonham Carter","alive":false,"image":"http://hp-api.herokuapp.com/images/bellatrix.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a8e"},"name":"Lord Voldemort","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"31-12-1926","yearOfBirth":{"$numberInt":"1926"},"ancestry":"half-blood","eyeColour":"red","hairColour":"bald","wand":{"wood":"yew","core":"phoenix feather","length":{"$numberDouble":"13.5"}},"patronus":"","hogwartsStudent":false,"hogwartsStaff":false,"actor":"Ralph Fiennes","alive":false,"image":"http://hp-api.herokuapp.com/images/voldemort.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a8f"},"name":"Horace Slughorn","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"","yearOfBirth":"","ancestry":"pure-blood","eyeColour":"green","hairColour":"blonde","wand":{"wood":"cedar","core":"dragon heartstring","length":{"$numberDouble":"10.25"}},"patronus":"","hogwartsStudent":false,"hogwartsStaff":true,"actor":"Jim Broadbent","alive":true,"image":"http://hp-api.herokuapp.com/images/slughorn.JPG"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a90"},"name":"Kingsley Shacklebolt","species":"human","gender":"male","house":"","dateOfBirth":"","yearOfBirth":"","ancestry":"pure-blood","eyeColour":"brown","hairColour":"brown","wand":{"wood":"","core":"","length":""},"patronus":"lynx","hogwartsStudent":false,"hogwartsStaff":false,"actor":"George Harris","alive":true,"image":"http://hp-api.herokuapp.com/images/kingsley.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a91"},"name":"Dolores Umbridge","species":"human","gender":"female","house":"Slytherin","dateOfBirth":"","yearOfBirth":"","ancestry":"half-blood","eyeColour":"brown","hairColour":"grey","wand":{"wood":"birch","core":"dragon heartstring","length":{"$numberInt":"8"}},"patronus":"persian cat","hogwartsStudent":false,"hogwartsStaff":true,"actor":"Imelda Staunton","alive":true,"image":"http://hp-api.herokuapp.com/images/umbridge.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a92"},"name":"Lucius Malfoy","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"","yearOfBirth":{"$numberInt":"1954"},"ancestry":"pure-blood","eyeColour":"grey","hairColour":"blonde","wand":{"wood":"elm","core":"dragon heartstring","length":{"$numberInt":"18"}},"patronus":"","hogwartsStudent":false,"hogwartsStaff":false,"actor":"Jason Isaacs","alive":true,"image":"http://hp-api.herokuapp.com/images/lucius.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a93"},"name":"Vincent Crabbe","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"","yearOfBirth":"","ancestry":"pure-blood","eyeColour":"black","hairColour":"black","wand":{"wood":"","core":"","length":""},"patronus":"","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Jamie Waylett","alive":false,"image":"http://hp-api.herokuapp.com/images/crabbe.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a94"},"name":"Gregory Goyle","species":"human","gender":"male","house":"Slytherin","dateOfBirth":"","yearOfBirth":"","ancestry":"pure-blood","eyeColour":"","hairColour":"brown","wand":{"wood":"","core":"","length":""},"patronus":"","hogwartsStudent":true,"hogwartsStaff":false,"actor":"Josh Herdman","alive":true,"image":"http://hp-api.herokuapp.com/images/goyle.jpg"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a95"},"name":"Mrs Norris","species":"cat","gender":"female","house":"","dateOfBirth":"","yearOfBirth":"","ancestry":"","eyeColour":"yellow","hairColour":"brown","wand":{"wood":"","core":"","length":""},"patronus":"","hogwartsStudent":false,"hogwartsStaff":true,"actor":"Maxime, Alanis and Tommy the cats","alive":true,"image":"http://hp-api.herokuapp.com/images/norris.JPG"}
{"_id":{"$oid":"5c7d656a82bb44be9e770a96"},"name":"Argus Filch","species":"human","gender":"male","house":"","dateOfBirth":"","yearOfBirth":"","ancestry":"squib","eyeColour":"","hairColour":"grey","wand":{"wood":"","core":"","length":""},"patronus":"","hogwartsStudent":false,"hogwartsStaff":true,"actor":"David Bradley","alive":true,"image":"http://hp-api.herokuapp.com/images/filch.jpg"}

我的嘗試:

  1. 顯示所有活着的角色(“alive”屬性等於 true)和學生(“hogwartsStudent”屬性等於 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());}
  2. 顯示“魔杖”屬性的“木頭”屬性為“冬青”的所有角色

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

對於這兩種情況,我只需要正確的 harrypotter.find 句子。 這樣做的其他方法對我不起作用,因為我的老師要求以這種方式這樣做。 謝謝!

您對您的方法的結果不夠清楚。 他們會給出執行錯誤還是沒有帶來您預期的結果?

對於第一個問題,好像alivehogwartsStudents都是boolean類型的,所以我推薦使用true而不是"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());}

對於第二個問題,我認為您應該使用fieldName.fieldProperty語法:

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());}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM