Retrieve only matched object from nested array in mongodb - mongodb

In this json , I need a find query which finds all the field where the "status":"Y", if the parent field has "status":"N", ignore the child field , else find the child field where the "status":"Y" along with its parent field
Note: The sub field is in a array
[
{
"type": "Type 1",
"status": "Y",
"code": "1",
"category": [
{
"type": "Cat 1",
"status": "Y",
"code": "1000",
"subcategories": [
{
"type": "Sub 1",
"status": "N",
"code": "1001"
},
{
"type": "Sub 2",
"status": "N",
"code": "1002"
},
{
"type": "Sub 3",
"status": "Y",
"code": "1003"
}
]
},
{
"type": "Cat 2",
"status": "N",
"code": "2000",
"subcategories": [
{
"type": "Sub 4",
"status": "Y",
"code": "2001"
},
{
"type": "Sub 5",
"status": "Y",
"code": "2002"
}
]
}
]
}
]
My Output Should be like this
[
{
"type": "Type 1",
"status": "Y",
"code": "1",
"category": [
{
"type": "Cat 1",
"status": "Y",
"code": "1000",
"subcategories": [
{
"type": "Sub 3",
"status": "Y",
"code": "1003"
}
]
} ]
}
]
Thanks in Advance:)

You can try below aggregation
db.collection.aggregate([
{ "$match": { "status": "Y" }},
{ "$unwind": "$category" },
{ "$match": { "category.status": "Y" } },
{ "$project": { "type": 1, "status": 1, "code": 1,
"category.type": "$category.type",
"category.status": "$category.status",
"category.code": "$category.code",
"category.subcategories": {
"$filter": {
"input": "$category.subcategories",
"as": "subcategory",
"cond": {
"$eq": [
"$$subcategory.status",
"Y"
]
}
}
}
}},
{ "$group": {
"_id": "$_id",
"type": { "$first": "$type" },
"status": { "$first": "$status" },
"code": { "$first": "$code" },
"category": { "$push": "$category" }
}}
]).then((data) => {
res.send(data)
})
Gives you following output (check here)
[
{
"_id": ObjectId("5a934e000102030405000000"),
"category": [
{
"code": "1000",
"status": "Y",
"subcategories": [
{
"code": "1003",
"status": "Y",
"type": "Sub 3"
}
],
"type": "Cat 1"
}
],
"code": "1",
"status": "Y",
"type": "Type 1"
}
]

Related

mongodb:complex nested aggregation

I have this collection:
[
{
"_id": {
"$oid": "60b22e1dbd46fa18a8308318"
},
"title": "basketball",
"price": 12,
"category": "Furniture",
"description": "",
"images": [
"http://res.cloudinary.com/hadarush100/image/upload/v1622289949/nfg948x3zro6gbiuknrz.jpg"
],
"categoryId": 1,
"userId": "60ad16493062eb11141d4927",
"createdAt": 1622289948232,
"chats": [
{
"id": 1,
"createdAt": 1622289948232,
"messages": [
{
"id": "1",
"createdAt": 1622289948232,
"senderId": "60ad16493062eb11141d4927",
"text": "Hello, Im the seller of this product."
}
]
},
{
"id": "2",
"createdAt": 1622289948232,
"messages": [
{
"id": 1,
"createdAt": 1622289948232,
"senderId": "60ad16493062eb11141d4927",
"text": "Hello, Im the seller of this product."
}
]
}
]
}
]
and i want to find specific document (by _id), then dive into specific chat in this document (by id), than use $lookup for replacing the "senderId" property in each message with a "sender" property that contains the full sender details (as a user), that exist in another collection (users). the result needs to look like this:
[
{
"_id": {
"$oid": "60b22e1dbd46fa18a8308318"
},
"title": "basketball",
"price": 12,
"category": "Furniture",
"description": "",
"images": [
"http://res.cloudinary.com/hadarush100/image/upload/v1622289949/nfg948x3zro6gbiuknrz.jpg"
],
"categoryId": 1,
"userId": "60ad16493062eb11141d4927",
"createdAt": 1622289948232,
"chats": [
{
"id": 1,
"createdAt": 1622289948232,
"messages": [
{
"id": "1",
"createdAt": 1622289948232,
"sender": {
"_id": {
"$oid": "60ad16493062eb11141d4927"
},
"username": "hadar",
"email": "hadarushha#gmail.com",
"profileImgUrl": "https://randomuser.me/api/portraits/men/79.jpg",
"createdAt": 1621956168518
},
"text": "Hello, Im the seller of this product."
}
]
},
{
"id": "2",
"createdAt": 1622289948232,
"messages": [
{
"id": 1,
"createdAt": 1622289948232,
"sender": {
"_id": {
"$oid": "60ad16493062eb11141d4927"
},
"username": "hadar",
"email": "hadarushha#gmail.com",
"profileImgUrl": "https://randomuser.me/api/portraits/men/79.jpg",
"createdAt": 1621956168518
},
"text": "Hello, Im the seller of this product."
}
]
}
]
}
]
You can use this aggregation:
$match to filter only selected document (_id)
$unwind multiple time to transform arrays into objects
$lookup to query external collection (users)
$group in reverse order
I assumed that your collections are more or less like this (next time, post both collections and also an example on a working playground)
db={
"products": [
{
"_id": {
"$oid": "60b22e1dbd46fa18a8308318"
},
"title": "basketball",
"price": 12,
"category": "Furniture",
"description": "",
"images": [
"http://res.cloudinary.com/hadarush100/image/upload/v1622289949/nfg948x3zro6gbiuknrz.jpg"
],
"categoryId": 1,
"userId": "60ad16493062eb11141d4927",
"createdAt": 1622289948232,
"chats": [
{
"id": 1,
"createdAt": 1622289948232,
"messages": [
{
"id": "1",
"createdAt": 1622289948232,
"senderId": "60ad16493062eb11141d4927",
"text": "Hello, Im the seller of this product."
}
]
},
{
"id": "2",
"createdAt": 1622289948232,
"messages": [
{
"id": 1,
"createdAt": 1622289948232,
"senderId": "60ad16493062eb11141d4927",
"text": "Hello, Im the seller of this product."
}
]
}
]
},
{
"_id": {
"$oid": "60b22e1dbd46fa18a8308319"
},
"title": "volleyball",
"price": 8,
"category": "Furniture",
"description": "",
"images": [
"http://res.cloudinary.com/hadarush100/image/upload/v1622289949/nfg948x3zro6gbiuknrz.jpg"
],
"categoryId": 1,
"userId": "60ad16493062eb11141d4927",
"createdAt": 1622289948232,
"chats": [
{
"id": 1,
"createdAt": 1622289948232,
"messages": [
{
"id": "1",
"createdAt": 1622289948232,
"senderId": "60ad16493062eb11141d4927",
"text": "Hello, Im the seller of this product."
}
]
},
{
"id": "2",
"createdAt": 1622289948232,
"messages": [
{
"id": 1,
"createdAt": 1622289948232,
"senderId": "60ad16493062eb11141d4928",
"text": "Hello, Im the seller of this product."
}
]
}
]
}
],
"users": [
{
"_id": {
"$oid": "60ad16493062eb11141d4927"
},
"username": "hadar",
"email": "hadarushha#gmail.com",
"profileImgUrl": "https://randomuser.me/api/portraits/men/79.jpg",
"createdAt": 1621956168518
},
{
"_id": {
"$oid": "60ad16493062eb11141d4928"
},
"username": "test",
"email": "test#gmail.com",
"profileImgUrl": "https://randomuser.me/api/portraits/men/49.jpg",
"createdAt": 1621956168528
},
]
}
And here is the working aggregation:
db.products.aggregate([
{
"$match": {
"_id": {
"$oid": "60b22e1dbd46fa18a8308319"
}
}
},
{
"$unwind": "$chats"
},
{
"$unwind": "$chats.messages"
},
{
"$addFields": {
"chats.messages.senderIdObjId": {
"$convert": {
"input": "$chats.messages.senderId",
"to": "objectId",
}
}
}
},
{
"$lookup": {
"from": "users",
"localField": "chats.messages.senderIdObjId",
"foreignField": "_id",
"as": "chats.messages.sender"
}
},
{
"$unwind": "$chats.messages.sender"
},
{
"$group": {
"_id": "$chats.id",
"messages": {
"$push": "$chats.messages"
},
"allFields": {
"$first": "$$ROOT"
}
}
},
{
"$addFields": {
"allFields.chats.messages": "$messages"
}
},
{
"$replaceWith": "$allFields"
},
{
"$group": {
"_id": "$_id",
"chats": {
"$push": "$chats"
},
"allFields": {
"$first": "$$ROOT"
}
}
},
{
"$addFields": {
"allFields.chats": "$chats"
}
},
{
"$replaceWith": "$allFields"
},
])
Working Playground here

mongodb agregate and filter data

I try to filter some results data from mongodb with mongoose in javascript.
This is my json structure:
{
"name": "john",
"firstname": "doe",
"yearold": 22,
"recipes": [
{
"title": "cheesecake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": 6,
"unit": "piece"
},
{
"name": "oil",
"label": "Specific oil",
"unit": "oz",
"value": 0.2
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 450,
"unit": "gr"
}
]
},
{
"title": "cake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": 6,
"unit": "piece"
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 500,
"unit": "gr"
},
]
}
]
}
In some case i need to return json data with hiding some values. For example I have a list that specifies all the values ​​to hide
hidekeys=["egg"];
and i would like to get this:
{
"name": "john",
"firstname": "doe",
"yearold": 22,
"recipes": [
{
"title": "cheesecake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": #######,
"unit": "piece"
},
{
"name": "oil",
"label": "Specific oil",
"unit": "oz",
"value": 0.2
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 450,
"unit": "gr"
}
]
},
{
"title": "cake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": #######,
"unit": "piece"
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 500,
"unit": "gr"
},
]
}
]
}
For each recipe i need to hide ingredient value if it is specified in hidekeys.
I tried something with $project and $cond but it doesnt works
Here's a quick way of how to achieve this using $map
const hidekeys = ["egg"];
db.collection.aggregate([
{
$addFields: {
recipes: {
$map: {
input: "$recipes",
as: "recipe",
in: {
$mergeObjects: [
"$$recipe",
{
data: {
$map: {
input: "$$recipe.data",
as: "datum",
in: {
"$mergeObjects": [
"$$datum",
{
$cond: [
{
"$setIsSubset": [
[
"$$datum.name"
],
hidekeys
]
},
{
value: "#####"
},
{
value: "$$datum.value"
}
]
}
]
}
}
}
}
]
}
}
}
}
}
])
Mongo Playground

Nested grouping of array

There are 3 master collection of category , subcategory and criteria each, i will be building framework with any possible combination of category , subcategory and criteria which will be stored as below-
framework document is added below having list of criteriaconfig as embedded object which further have single object of category , subcategory and criteria. you can refer criteriaconfig as link table that u call in mysql.
[
{
"id": "592bc3059f3ad715002b2331",
"name": "Framework1",
"description": "framework 1 for testing",
"criteriaConfigs": [
{
"id": "592bc3059f3ad715002b232f",
"category": {
"id": "591c2f5faa187956b2d0fb39",
"name": "category1",
"description": "category1",
"deleted": false,
"createdDate": 1495019359558
},
"subCategory": {
"id": "591c2f5faa187956b2d0fb83",
"name": "subCat1",
"description": "subCat1"
},
"criteria": {
"id": "591c2f5faa187956b2d0fbad",
"name": "criteria1",
"measure": "Action"
}
},
{
"id": "592bc3059f3ad715002b232e",
"category": {
"id": "591c2f5faa187956b2d0fb37",
"name": "Process",
"description": "Enagagement"
},
"subCategory": {
"id": "591c2f5faa187956b2d0fb81",
"name": "COMM / BRANDING",
"description": "COMM / BRANDING"
},
"criteria": {
"id": "591c2f5faa187956b2d0fba9",
"name": "Company representative forgets about customer on hold",
"measure": ""
}
} ]
},
{
"id": "592bc3059f3ad715002b2332",
"name": "Framework2",
"description": "framework 2 for testing",
"criteriaConfigs": [
{
"id": "592bc3059f3ad715002b232f",
"category": {
"id": "591c2f5faa187956b2d0fb39",
"name": "category1",
"description": "category1"
},
"subCategory": {
"id": "591c2f5faa187956b2d0fb83",
"name": "subCat1",
"description": "subCat1"
},
"criteria": {
"id": "591c2f5faa187956b2d0fbad",
"name": "criteria1",
"measure": "Action"
}
}
]
}
]
i need a view containing framework that will contain all list of category and inside category there will be list of added subcategory and inside subcategory will have list of criteria for single framework.
expected result -
[
{
"id": "f1",
"name": "Framework1",
"description": "framework 1 for testing",
"categories": [
{
"id": "c2",
"name": "category2",
"description": "category2",
"subCategories": [
{
"id": "sb1",
"name": "subCat1",
"description": "subCat1",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr2",
"name": "criteria2",
"measure": "Action"
},
{
"id": "cr3",
"name": "criteria3",
"measure": "Action"
}]
},
{
"id": "sb2",
"name": "subCat2",
"description": "subCat2",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr4",
"name": "criteria4",
"measure": "Action"
}]
}]
},
{
"id": "c1",
"name": "category1",
"description": "category1",
"subCategories": [
{
"id": "sb3",
"name": "subCat3",
"description": "subCat3",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr2",
"name": "criteria2",
"measure": "Action"
}
]},
{
"id": "sb2",
"name": "subCat2",
"description": "subCat2",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr4",
"name": "criteria4",
"measure": "Action"
}]
}
]
}]
},
{
"id": "f2",
"name": "Framework2",
"description": "framework 2 for testing",
"categories": [
{
"id": "c2",
"name": "category2",
"description": "category2",
"subCategories": [
{
"id": "sb4",
"name": "subCat5",
"description": "subCat5",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr3",
"name": "criteria3",
"measure": "Action"
}]
},
{
"id": "sb2",
"name": "subCat2",
"description": "subCat2",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr4",
"name": "criteria4",
"measure": "Action"
}]
}]
},
{
"id": "c1",
"name": "category1",
"description": "category1",
"subCategories": [
{
"id": "sb3",
"name": "subCat3",
"description": "subCat3",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr2",
"name": "criteria2",
"measure": "Action"
}
]},
{
"id": "sb2",
"name": "subCat2",
"description": "subCat2",
"criterias": [
{
"id": "cr1",
"name": "criteria1",
"measure": "Action"
},
{
"id": "cr4",
"name": "criteria4",
"measure": "Action"
}]
}
]
}]
}
]
Note - Category document doesn't have any reference to subcategory and same way subcategory doesn't have any reference to criteria object currently as they are master data and are generic , framework is created with their combination dynamically.
If you want to try to do all the work in the aggregation, you could group first by subcategory, then by category like:
db.collection.aggregate([
{$unwind:"$criteriaConfigs"},
{$project:{
_id:0,
category:"$criteriaConfigs.category",
subCategory:"$criteriaConfigs.subCategory",
criteria:"$criteriaConfigs.criteria"
}},
{$group:{
_id:{"category":"$category","subCategory":"$subCategory"},
criteria:{$addToSet:"$criteria"}
}},
{$group:{
_id:{"category":"$_id.category"},
subCategories:{$addToSet:{subCategory:"$_id.subCategory",
criteria:"$criteria"}}
}},
{$project:{
_id:0,category:"$_id.category",
subCategories:"$subCategories"
}}
])
Depending on how you plan to us the return data, it may be more efficient to return each unique combination:
db.collection.aggregate([
{$unwind:"$criteriaConfigs"},
{$group:{
_id:{
category:"$criteriaConfigs.category.name",
subCategory:"$criteriaConfigs.subCategory.name",
criteria:"$criteriaConfigs.criteria.name"
}
}},
{$project:{
_id:0,
category:"$_id.category",
subCategory:"$_id.subCategory",
criteria:"$_id.criteria"
}}
])
I'm not sure from your question what shape you are expecting the return data to have, so you may need to adjust for that.

REST API 'Create Passenger Name Record' Warning and errors

The below attached warnings and error occurred while testing Booking seat.
There is no any proper documentation of Create Passenger Name Record REST API Call, the description and schema are meaning less. In description there are 266 parameters are required true to send a request.
Do you have any proper documentation where i can get all the required parameters detailed information? Like What is SegmentNumber how can i get?
Working flow( For Single trip) :
Get the origin, destination, date, number of seats required.
Send all required parms to Bargain Finder Max, get the response
Send Require params to book a seat. Create Passenger Name Record
Request
{
"CreatePassengerNameRecordRQ": {
"targetCity": "3QND",
"Profile": {
"UniqueID": {
"ID": "ABCD1EF"
}
},
"AirBook": {
"OriginDestinationInformation": {
"FlightSegment": [{
"ArrivalDateTime": "2017-04-30",
"DepartureDateTime": "2017-04-30T13:55",
"FlightNumber": "309",
"NumberInParty": "1",
"ResBookDesigCode": "V",
"Status": "NN",
"DestinationLocation": {
"LocationCode": "KHI"
},
"MarketingAirline": {
"Code": "PK",
"FlightNumber": "309"
},
"MarriageGrp": "O",
"OriginLocation": {
"LocationCode": "ISB"
}
}]
}
},
"AirPrice": {
"PriceRequestInformation": {
"OptionalQualifiers": {
"MiscQualifiers": {
"TourCode": {
"Text": "TEST1212"
}
},
"PricingQualifiers": {
"PassengerType": [{
"Code": "CNN",
"Quantity": "1"
}]
}
}
}
},
"MiscSegment": {
"DepartureDateTime": "2017-04-30",
"NumberInParty": 1,
"Status": "NN",
"Type": "OTH",
"OriginLocation": {
"LocationCode": "ISB"
},
"Text": "TEST",
"VendorPrefs": {
"Airline": {
"Code": "PK"
}
}
},
"SpecialReqDetails": {
"AddRemark": {
"RemarkInfo": {
"FOP_Remark": {
"Type": "CHECK",
"CC_Info": {
"Suppress": true,
"PaymentCard": {
"AirlineCode": "PK",
"CardSecurityCode": "1234",
"Code": "VI",
"ExpireDate": "2012-12",
"ExtendedPayment": "12",
"ManualApprovalCode": "123456",
"Number": "4123412341234123",
"SuppressApprovalCode": true
}
}
},
"FutureQueuePlaceRemark": {
"Date": "12-21",
"PrefatoryInstructionCode": "11",
"PseudoCityCode": "IPCC1",
"QueueIdentifier": "499",
"Time": "06:00"
},
"Remark": [{
"Type": "Historical",
"Text": "TEST HISTORICAL REMARK"
},
{
"Type": "Invoice",
"Text": "TEST INVOICE REMARK"
},
{
"Type": "Itinerary",
"Text": "TEST ITINERARY REMARK"
},
{
"Type": "Hidden",
"Text": "TEST HIDDEN REMARK"
}]
}
},
"AirSeat": {
"Seats": {
"Seat": [{
"NameNumber": "1.1",
"Preference": "AN",
"SegmentNumber": "0"
},
{
"NameNumber": "2.1",
"Preference": "AN",
"SegmentNumber": "1"
},
{
"NameNumber": "3.1",
"Preference": "AN",
"SegmentNumber": "1"
}]
}
},
"SpecialService": {
"SpecialServiceInfo": {
"Service": [{
"SSR_Code": "OSI",
"PersonName": {
"NameNumber": "testing"
#},
"Text": "TEST1",
"VendorPrefs": {
"Airline": {
"Code": "PK"
}
}
}]
}
}
},
"PostProcessing": {
"RedisplayReservation": true,
"ARUNK": "",
"QueuePlace": {
"QueueInfo": {
"QueueIdentifier": [{
"Number": "100",
"PrefatoryInstructionCode": "11"
}]
}
},
"EndTransaction": {
"Source": {
"ReceivedFrom": "SWS TEST"
}
}
}
}
}
Response:
{
"CreatePassengerNameRecordRS": {
"ApplicationResults": {
"status": "NotProcessed",
"Error": [
{
"type": "Application",
"timeStamp": "2017-03-08T04:10:41.317-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "ERR.SP.BUSINESS_ERROR",
"content": "PNR has not been created successfully, see remaining messages for details"
}
]
}
]
}
],
"Warning": [
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:40.628-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SP.PROVIDER_ERROR",
"content": "NO PROFILE FOUND FOR NAME"
}
]
}
]
},
{
"type": "Validation",
"timeStamp": "2017-03-08T04:10:40.655-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.CLIENT.VALIDATION_FAILED",
"content": "Request contains incorrect values: Wrong dateTime format"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:40.919-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": "FORMAT, CHECK SEGMENT NUMBER-0003"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:41.024-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": ".DTE.NOT ENT BGNG WITH"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:41.062-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": "\u0087ND NAMES\u0087"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:41.096-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": "\u0087ND NAMES\u0087"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:41.129-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": "\u0087ND NAMES\u0087"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:41.166-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": "NO ARNK INSERTED"
}
]
}
]
},
{
"type": "BusinessLogic",
"timeStamp": "2017-03-08T04:10:41.229-06:00",
"SystemSpecificResults": [
{
"Message": [
{
"code": "WARN.SWS.HOST.ERROR_IN_RESPONSE",
"content": "NEED PHONE FIELD - USE 9"
}
]
}
]
}
]
},
"TravelItineraryRead": {
"TravelItinerary": {
"CustomerInfo": {
},
"ItineraryInfo": {
"ReservationItems": {
"Item": [
{
"RPH": "1",
"MiscSegment": {
"DayOfWeekInd": "7",
"DepartureDateTime": "04-30",
"NumberInParty": "01",
"SegmentNumber": "0001",
"Status": "NN",
"Type": "OTH",
"IsPast": false,
"OriginLocation": {
"LocationCode": "ISB"
},
"Text": [
"TEST"
],
"Vendor": {
"Code": "PK"
}
}
}
]
}
},
"ItineraryRef": {
"AirExtras": false,
"InhibitCode": "U",
"PartitionID": "AA",
"PrimeHostID": "1B",
"Header": [
"CURRENTLY DISPLAYING A PNR OWNED BY THE SABRE PRIME HOST",
"RULES AND FUNCTIONALITY FOR THAT PRIME HOST WILL APPLY"
],
"Source": {
"PseudoCityCode": "3QND",
"ReceivedFrom": "SWS TEST"
}
},
"SpecialServiceInfo": [
{
"RPH": "001",
"Type": "GFX",
"Service": {
"SSR_Code": "OSI",
"Airline": {
"Code": "PK"
},
"Text": [
"TEST1-TESTING"
]
}
}
]
}
}
},
"Links": [
{
"rel": "self",
"href": "https:\/\/api.sabre.com\/v1.0.0\/passenger\/records?mode=create"
},
{
"rel": "linkTemplate",
"href": "https:\/\/api.sabre.com\/\/passenger\/records?mode="
}
]
}
Please avoid posting the same questions. Here's an answer I just posted regarding the required elements: https://stackoverflow.com/a/42671412/3701641
About the segment number, they represent the itinerary segments, you are adding one flight segment, so the segment number associated with that would be 1.

Grouping select with partial

Hello i have here this code:
public function getConversations($user, $limit, $page)
{
$offset = ($page - 1) * $limit;
$qb = $this->createQueryBuilder('message');
$qb
->select('message, partial o.{id}, partial userFrom.{id, username, avatar}, partial userTo.{id, username, avatar}, partial availability.{id, timeFrom}')
->leftJoin('message.from', 'userFrom')
->leftJoin('message.to', 'userTo')
->leftJoin('message.order', 'o')
->leftJoin('o.availabilities', 'availability')
->where($qb->expr()->orX(
$qb->expr()->eq('message.from', ':user'),
$qb->expr()->eq('message.to', ':user')
))
->setParameter('user', $user)
->setFirstResult($offset)
->setMaxResults($limit)
->addOrderBy('message.created', Criteria::DESC);
return $qb->getQuery()->getArrayResult();
}
I want to get only last message connected to each order. If i have 2 messages to order it gives me 2 rows but i need only one (the last one). I tried to use GROUP BY but it didn't work well. It wanted me to put all foreign ids to group by and then the result was really bad.
Result for this is:
{
"id": "813fa986-f89b-411d-8324-727e427cbc01",
"isRead": false,
"created": {
"date": "2016-12-12 18:01:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Hello to you too",
"contentType": "text",
"from": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"to": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
},
{
"id": "813fa986-f89b-411d-8324-727e427cbc00",
"isRead": false,
"created": {
"date": "2016-12-12 18:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Helllooooooo",
"contentType": "text",
"from": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"to": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
and should be just like this:
{
"id": "813fa986-f89b-411d-8324-727e427cbc01",
"isRead": false,
"created": {
"date": "2016-12-12 18:01:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Hello to you too",
"contentType": "text",
"from": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"to": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
}