当前位置: 首页 > 知识库问答 >
问题:

从mongodb中的集合获取列表

阴雪风
2023-03-14

我有用于数据库的Spring Boot App和MongoDB。

在数据库中,我有一个集合packageholiday,其中有一些元素,这是JSON:

    [{
  "_id": {
    "$oid": "62bf6a6121d6c6a226d6b418"
  },
  "title": "Cambodia",
  "description": "Cambodia, a country located in Southeast Asia, is bordered by the Lao People's Democratic Republic to the northeast, Thailand to the northwest, Vietnam to the east, and the Gulf of Thailand to the southwest. Cambodia gained its independence in 1953 and is a member of the Association of South-East Asia Nations (ASEAN). Cambodia has a multiparty democracy under a constitutional monarchy, with Phnom Penh as the capital. Cambodia’s official language is Khmer, and the currency is the riel (KHR).",
  "whatToDo": "Start off by exploring the temples of Angkor Wat while you base yourself in the town of Siem Reap. If you’re into adventuring and going at it alone, rent a bike and get your exercise sweating your way through these 12th century ruins. Alternatively, sign up with one of the hostel tours to get a feel for the temples and meet some other wannabe Indiana Jones travellers along the way. From Siem Reap, don’t miss out on Cambodia’s capital Phnom Penh to learn about the country’s difficult history in the 1970s through a visit its jarring museums, S21 and the Killing Fields. There are plenty of great cafes and restaurants in the city that were made for solo travellers just like you! Then get your water fix by heading down to the islands off of Sihanoukville for some serious beach time and partying, or to the riverside haven of Kampot for kayaking and great coffee. ",
  "packageHolidayType": [
    "indepedentTours"
  ]
},{
  "_id": {
    "$oid": "62bf6b5521d6c6a226d6b41b"
  },
  "title": "Myanmar",
  "description": "The Republic of the Union of Myanmar (or more commonly known as Myanmar) has an area of 676,578 square kilometres, making it the second largest country in Southeast Asia. It shares borders with Bangladesh, China, India, Laos, and Thailand. Naypyidaw is the capital of Myanmar, and Yangon, as the former capital, is the country’s most important commercial centre.",
  "whatToDo": "Head to the most famous place in Myanmar, Bagan, where you can rent out an e-bike (solo or with a group from your hostel) to explore over 2,000 temples and pagodas. You can also hop in some shared transport to visit the nearby Mount Popa, a monastery perched on top of an extinct volcano with 777 stairs to climb and maybe even more monkeys to get past. From Bagan you can head to Hsipaw or Kalaw that act as starting points for multi-day treks to see Myanmar’s countryside and rest your head in homestays along the way. Treks are also a great way to meet new travel friends. And if you’re craving a bit of reflective solo time? You can visit places like the enchanting Shwedagon Pagoda in Yangon or Mandalay’s U Bein Bridge where flocks of people congregate every sunrise and sunset. Both are magical places to explore solo, although you’re likely to get invited to join a local family for a snack at both.",
  "packageHolidayType": [
    "indepedentTours"
  ]
},{
  "_id": {
    "$oid": "62bf6d0d21d6c6a226d6b41d"
  },
  "title": "Classical Spain",
  "description": "Experience Andalucía’s magical fusion of Spanish and Moorish cultures as you discover Seville’s vibrant streets and conquistador heritage, Granada’s wondrous Alhambra and the dazzling Mezquita mosque in Córdoba – and much more. Andalucía is simply one of the most beautiful corners of Europe yet for many it remains relatively undiscovered. It’s a land where the excesses of the modern era seem not to have taken root and where travellers are genuinely welcomed. The food is a delight, with the area noted for its seafood and its famous drink, the fortified wine from Jerez we call sherry. Try a glass of fino, a very dry yet refreshing sherry, while nibbling tapas, that great Spanish titbit invention. The people are charming – always ready to help, with a friendly attitude towards their fellow man and a love of life so typical of Mediterranean people. So, for an inspirational blend of timeless countryside, iconic monuments, romantic and vivacious cities, fragrant flowers and orange blossom, wonderful art and superb cuisine, all set in a region that welcomes visitors as honoured guests, welcome to Andalucía.",
  "packageHolidayType": [
    "escortedTours"
  ]
},{
  "_id": {
    "$oid": "62bf6e08d4a37b95af66f59f"
  },
  "title": "Japan",
  "description": "Japan - an ancient culture of emperors, fierce shoguns, samurais and brutal martial arts. Yet it’s also a place of gentle geishas, colourful kimonos, exquisite garden design and Zen Buddhism. How did a country so deeply rooted in such diverse heritage become one of the most modern and advanced economies on the planet? Pondering such questions is what makes a trip to Japan so incredibly rewarding. Having travelled there once, you’ll feel almost hypnotically drawn back to this truly enigmatic country.\n\nJapan’s roots lie in an ancient feudal culture, with many customs from these times still pervading modern society. The Portuguese and Jesuit missionaries arrived in the 16th century and by all accounts, they were initially treated well, mainly because the Japanese were impressed with their new technology – firearms. Despite trading together successfully for nearly a century, Christianity and western ideas were deemed to be undermining the social order and the Portuguese were expelled. All contact with outsiders was banned for over 200 years. The only country on earth to have been so completely isolated from all external influences, Japanese art and culture developed in its own unique way, until pressure from the US finally broke down the barriers in the 1850’s.",
  "packageHolidayType": [
    "escortedTours"
  ]
}]

我的目标是什么:

正如你们所见,我在集合中有这样的数组:

 "packageHolidayType": [
    "indepedentTours" //type
  ]



    "packageHolidayType": [
    "escortedTours" //type
  ]

所以我的问题实际上是创建API,它将从集合中返回具有指定packageHolidayType的元素。

例如

packageHolidayType的API

@GetMapping("/getEscortedTours")
....
How to filter here just that type

例如,对于相同的

packageHolidayType的API

@GetMapping("/getIndepedentTours")
....
How to filter here just that type

我怎样才能做到这一点?这样可能吗?

共有1个答案

田宇
2023-03-14

在查询中使用Spring数据

http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repository-query-keywords

以下实现适用于上述用例:

@Repository
public interface PackageHolidayRepository extends MongoRepository<PackageHoliday, String> {
     List<PackageHoliday> findByPackageHolidayTypeIn(List<String> packageHolidayTypes);
}

或者,您可以将查询与包含一起使用:

 @Query("{ packageHolidayType: { $all: ?0 } }")
 List<PackageHoliday> findByackageHolidayType(String packageHolidayType);
 类似资料:
  • 问题内容: 我有一个带有多个对象的集合“ companies”。每个对象都有“ _id”参数。我正在尝试从数据库获取此参数: 因此,我请求公司/ 4fcfd7f246e1464d05000001(4fcfd7f246e1464d05000001是我需要的对象的_id- parma),并且findOne不返回任何内容,这就是为什么console.log(’该公司没有数据’); 执行。 我绝对确定我有

  • 问题内容: 我的Firestore数据库结构: 现在我想从中获取文件清单。将有一个包含以下数据的列表。我需要它是因为我想制作一个微调器并将这些数据放入微调器中。然后,用户将选择一些文档并下载。 我尝试使用下面的代码: 但是代码只有在我拥有没有文档集合的数据结构时才起作用。在其他情况下,中没有任何文档。 谢谢! 问题答案: 要在中包含所有文档名称的列表,请使用以下代码: 您的logcat中的结果将是

  • 我的问题与[这里][1]相同。我已经创建了一个索引“

  • 问题内容: 它是纠正与Java 8,你需要执行下面的代码确实获得从平行流Collection? 从CollectionAPI: 默认Stream parallelStream() 返回一个可能与此流作为其源的并行Stream。此方法允许返回顺序流。 从BaseStreamAPI: S parallel() 返回并行的等效流。可能由于流已经是并行的,或者因为基础流的状态被修改为并行而返回自身。 我需

  • 在Java8中,您需要执行以下代码才能从中获得并行流,这是否正确? 来自API: 默认流parallelStream()

  • 我正在使用MongoDB 2.6标准。我已经成功地创建了多个集合,插入数据,查询数据等。在Mongo shell内部工作良好,以及从使用MongoSkin的NodeJS应用程序。到目前为止,一切都很好。现在我需要使用第三方工具(D2RQ)来访问数据。似乎D2RQ使用_schema集合来获取集合名称,列名,数据类型等。D2RQ适用于其中三个集合,因为集合在MongoDB中_schema。第四个集合不