i'm new in mongodb. have following data json format in mongodb. need search booklabel or shortlabel book , should show me information book. example: if query 'cosmos' it'll show description book, like: booklabel, writer, yearpublish, url. how can in java? need query, please help.
"class":"science", "description":[ { "booklabel":"cosmos (mass market paperback)", "shortlabel":"cosmos", "writer":"carl sagan", "yearpublish":[ "2002" ], "url":"https://www.goodreads.com/book/show/55030.cosmos" }, { "booklabel":"the immortal life of henrietta lacks", "shortlabel":"immortal life", "writer":"rebecca skloot", "yearpublish":[ "2010, 2011" ], "url":"https://www.goodreads.com/book/show/6493208-the-immortal-life-of-henrietta-lacks" } ], "class":"history", "description":[ { "booklabel":"the rise , fall of third reich", "shortlabel":"rise , fall", "writer":"william l. shirer", "yearpublish":[ "1960" ], "url":"https://www" } ]
}
with mongodb java driver v3.2.2 can this:
finditerable<document> iterable = collection.find(document.parse("{\"description.shortlabel\": {$regex: \"cosmos\"}"));
this returns documents containing cosmos
in description.shortlabel
nested field. exact match, try {"description.shortlabel": "cosmos"}
. replace shortlabel
booklabel
to search booklabel
field. can iterable.foreach(new block<document>())
on returned documents. search both booklabel
, shortlabel
, can $or{}
. syntax wrong check mongodb manual. general idea.
Comments
Post a Comment