根据您的需求,可以新增一个 "pricing" 对象来存储每张订单或每个运输的定价信息。以下是更新后的 Couchbase N1QL 语句示例:
创建表:
CREATE BUCKET dangerous_goods;
插入 Transportation 数据:
INSERT INTO dangerous_goods (KEY, VALUE) VALUES
("transport_1", {
"id": "transport_1",
"stage_id": 1,
"type": "vehicle",
"vehicle_plate_number": "A12345",
"start_time": "2022-01-01 08:00:00",
"end_time": "2022-01-01 10:00:00",
"driver": "John",
"vehicle_issue": "",
"pricing": {
"type": "vehicle",
"price": 1000
}
}),
("transport_2", {
"id": "transport_2",
"stage_id": 2,
"type": "outsourcing",
"transport_company_id": "company_1",
"start_time": "2022-01-01 12:00:00",
"end_time": "2022-01-02 10:00:00",
"driver": "Mike",
"vehicle_issue": "Tire punctured",
"pricing": {
"type": "outsourcing",
"price": 2000
}
}),
("transport_3", {
"id": "transport_3",
"stage_id": 3,
"type": "vehicle",
"vehicle_plate_number": "C12345",
"start_time": "2022-01-02 12:00:00",
"end_time": "2022-01-03 08:00:00",
"driver": "Lucy",
"vehicle_issue": "",
"pricing": {
"type": "vehicle",
"price": 1500
}
});
插入订单数据:
INSERT INTO dangerous_goods (KEY, VALUE) VALUES
("order_1", {
"id": "order_1",
"customer_id": "customer_1",
"start_location": "Shanghai",
"end_location": "Beijing",
"dangerous_goods_type": "Flammable Liquids",
"quantity": 100,
"pricing": {
"price": 3000
},
"transportation": {
"1": {
"transport_id": "transport_1",
"pricing": {
"type": "vehicle",
"price": 1000
}
},
"2": {
"transport_id": "transport_2",
"pricing": {
"type": "outsourcing",
"price": 2000
}
},
"3": {
"transport_id": "transport_3",
"pricing": {
"type": "vehicle",
"price": 1500
}
}
}
});
更新数据:
UPDATE dangerous_goods AS orders
SET orders.pricing.price = 4000,
orders.transportation.1.pricing.price = 1200,
orders.transportation.2.pricing.price = 2200
WHERE orders.id = "order_1"
AND orders.type = "order";
查询数据:
SELECT *
FROM dangerous_goods AS orders
JOIN dangerous_goods AS transports ON KEYS orders.transportation.1.transport_id
WHERE orders.id = "order_1" AND orders.type = "order";
删除数据:
DELETE FROM dangerous_goods USE KEYS ["order_1"];
更多的 N1QL 语句示例可以在 Couchbase 的官方文档中找到。