我正在根据酒店搜索测试一个场景。 所以我制作了3个模型类,分别是酒店,房间和可用性
以下是我们正在使用的三个模型类。
Hotel.java
@Document(collection =“Hotels”) 公共级酒店{
@Id
private String id;
private String name;
private Long pincode;
@Field("roomCategoryDocs")
private Object roomCategoryDocs;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPincode() {
return pincode;
}
public void setPincode(Long pincode) {
this.pincode = pincode;
}
public Object getRoomCategoryDocs() {
return roomCategoryDocs;
}
public void setRoomCategoryDocs(Object roomCategoryDocs) {
this.roomCategoryDocs = roomCategoryDocs;
}
@Override
public String toString() {
return String.format("Hotel [id=%s, name=%s, pincode=%s, roomCategoryDocs=%s]", id, name, pincode,
roomCategoryDocs);
}
}
Room.java
@Document(collection =“rooms”) 公共课室{
@Id
private String id;
private String frontImg;
private String category;
private ObjectId hotelId;
@Field("availabilityDocs")
private Object availabilityDocs;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFrontImg() {
return frontImg;
}
public void setFrontImg(String frontImg) {
this.frontImg = frontImg;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public ObjectId getHotelId() {
return hotelId;
}
public void setHotelId(ObjectId hotelId) {
this.hotelId = hotelId;
}
public Object getAvailabilityDocs() {
return availabilityDocs;
}
public void setAvailabilityDocs(Object availabilityDocs) {
this.availabilityDocs = availabilityDocs;
}
@Override
public String toString() {
return String.format("Room [id=%s, frontImg=%s, category=%s, hotelId=%s, availabilityDocs=%s]", id, frontImg,
category, hotelId, availabilityDocs);
}
}
Availability.java
@Document(collection =“rooms”) 公共类可用性{
@Id
private String id;
private ObjectId roomId;
private Integer availability;
private Double rate;
private String date;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ObjectId getRoomId() {
return roomId;
}
public void setRoomId(ObjectId roomId) {
this.roomId = roomId;
}
public Integer getAvailability() {
return availability;
}
public void setAvailability(Integer availability) {
this.availability = availability;
}
public Double getRate() {
return rate;
}
public void setRate(Double rate) {
this.rate = rate;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return String.format("Availability [id=%s, roomId=%s, availability=%s, rate=%s, date=%s]", id, roomId,
availability, rate, date);
}
}
这些存储在两个mongodb集合中,一个用于酒店,另一个用于房间和可用性。
下面我们有Controller类的方法,我们从
获取数据@GetMapping(value =“/ hotel”) public List getAllHotel(){
Aggregation agg = newAggregation(match(Criteria.where("pincode").lt(800080)),
lookup("rooms", "_id", "hotelId", "roomCategoryDocs"), unwind("roomCategoryDocs", true),
lookup("rooms", "roomCategoryDocs._id", "roomId", "roomCategoryDocs.availabilityDocs"),
unwind("roomCategoryDocs", true),
group("_id").first("name").as("name").last("pincode").as("pincode").push("roomCategoryDocs").as("roomCategoryDocs")
);
// Convert the aggregation result into a List
AggregationResults<Hotel> groupResults = mongoTemplate.aggregate(agg, "Hotels", Hotel.class);
List<Hotel> result = groupResults.getMappedResults();
return result;
}
现在,在获取酒店数据时,我们将获得房间和可用性的所有数据。
我们得到的输出:
[
{
"id": "59fabce1acd8be143417b101",
"name": "Elegance",
"pincode": 700042,
"availabilityDocs": [
{
"id": "59fabe39acd8be143417b103",
"frontImg": "google.com/1234",
"category": "Deluxe",
"hotelId": {
"timestamp": 1509604577,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552641,
"timeSecond": 1509604577,
"time": 1509604577000,
"date": 1509604577000
},
"availabilityDocs1": [
{
"id": "59fabf66acd8be14344a2df3",
"roomId": {
"timestamp": 1509604921,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552643,
"timeSecond": 1509604921,
"time": 1509604921000,
"date": 1509604921000
},
"availability": 10,
"rate": 4000,
"date": "2017-10-11"
},
{
"id": "59fae259acd8be264cf4430f",
"roomId": {
"timestamp": 1509604921,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552643,
"timeSecond": 1509604921,
"time": 1509604921000,
"date": 1509604921000
},
"availability": 11,
"rate": 4001,
"date": "2017-10-12"
}
]
}
]
}
]
但现在我们陷入了一个问题,即根据日期属性进行搜索。
预期产出:
这里我们只想从11-10-2017获取数据。
{
"id": "59fabce1acd8be143417b101",
"name": "Elegance",
"pincode": 700042,
"availabilityDocs": [
{
"id": "59fabe39acd8be143417b103",
"frontImg": "google.com/1234",
"category": "Deluxe",
"hotelId": {
"timestamp": 1509604577,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552641,
"timeSecond": 1509604577,
"time": 1509604577000,
"date": 1509604577000
},
"availabilityDocs1": [
{
"id": "59fabf66acd8be14344a2df3",
"roomId": {
"timestamp": 1509604921,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552643,
"timeSecond": 1509604921,
"time": 1509604921000,
"date": 1509604921000
},
"availability": 10,
"rate": 4000,
"date": "2017-10-11"
}
}
下面是json集合示例 酒店系列:
{
"_id" : ObjectId("59fabce1acd8be143417b101"), "name" : "Elegance", "pincode" :
NumberLong(700042), "_class" : "com.lara.mongodb.domain.Hotel"
}
客房收藏示例:
{
"_id" : ObjectId("59fabf66acd8be14344a2df3"),
"roomId" : ObjectId("59fabe39acd8be143417b103"),
"availability" : 10,
"rate" : 4000,
"date" : "2017-10-11",
"_class" : "com.lara.mongodb.domain.Availability"
}
我对任何有关设计的建议以及您可能拥有的任何其他想法持开放态度,因为我是新手。 任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
老实说,我对你如何在同一个系列中写两个实体感到有些困惑。从你添加的示例集合中,我可以看到一个缺失。但把它放在一边。我使用你的最终结果作为切入点。所以我从:
开始{
"id": "59fabce1acd8be143417b101",
"name": "Elegance",
"pincode": 700042,
"availabilityDocs": [
{
"id": "59fabe39acd8be143417b103",
"frontImg": "google.com/1234",
"category": "Deluxe",
"hotelId": {
"timestamp": 1509604577,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552641,
"timeSecond": 1509604577,
"time": 1509604577000,
"date": 1509604577000
},
"availabilityDocs1": [
{
"id": "59fabf66acd8be14344a2df3",
"roomId": {
"timestamp": 1509604921,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552643,
"timeSecond": 1509604921,
"time": 1509604921000,
"date": 1509604921000
},
"availability": 10,
"rate": 4000,
"date": "2017-10-11"
},
{
"id": "59fae259acd8be264cf4430f",
"roomId": {
"timestamp": 1509604921,
"machineIdentifier": 11327678,
"processIdentifier": 5172,
"counter": 1552643,
"timeSecond": 1509604921,
"time": 1509604921000,
"date": 1509604921000
},
"availability": 11,
"rate": 4001,
"date": "2017-10-12"
}
]
}
]
}
所以这是你做过聚合后的结果。现在您只需要2017-10-11
可用的房间。但是在你的结果数组中你得到了&#34; 2017-10-12&#34;和&#34; 2017-10-11&#34;。
因此,如果我有上述结果,您必须添加以下内容:
{
$unwind: "$availabilityDocs"
},
{
$unwind: "$availabilityDocs.availabilityDocs1"
},
{
$match: {
"availabilityDocs.availabilityDocs1.date" : "2017-10-11"
}
}