Filter nested array with conditions based on multi-level object values and update them - MongoDB aggregate + update - mongodb

Considering I have the following documents in a collection (ignoring the _id) :
[
{
"Id": "OP01",
"Sessions": [
{
"Id": "Session01",
"Conversations": [
{
"Id": "Conversation01",
"Messages": [
{
"Id": "Message01",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message02",
"Status": "delivered",
"Direction": "internal"
},
{
"Id": "Message03",
"Status": "delivered",
"Direction": "inbound"
},
{
"Id": "Message04",
"Status": "sent",
"Direction": "outbound"
}
]
},
{
"Id": "Conversation02",
"Messages": [
{
"Id": "Message05",
"Status": "sent",
"Direction": "outbound"
}
]
}
]
},
{
"Id": "Session02",
"Conversations": [
{
"Id": "Conversation03",
"Messages": [
{
"Id": "Message06",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message07",
"Status": "delivered",
"Direction": "internal"
}
]
},
{
"Id": "Conversation04",
"Messages": []
}
]
}
]
},
{
"Id": "OP02",
"Sessions": [
{
"Id": "Session03",
"Conversations": []
}
]
},
{
"Id": "OP03",
"Sessions": []
}
]
First query — aggregate (+$project)
I want to get the list of Messages grouped by their Conversations where:
Sessions.Id: "Session01"
and
Sessions.Conversations.Messages.Direction $in ["inbound", "outbound"]
and
Sessions.Conversations.Messages.Status $in ["sent", "delivered"]
The expected result is:
[
{
"Id": "Conversation01",
"Messages": [
{
"Id": "Message03",
"Status": "delivered",
"Direction": "inbound"
},
{
"Id": "Message04",
"Status": "sent",
"Direction": "outbound"
}
]
},
{
"Id": "Conversation02",
"Messages": [
{
"Id": "Message05",
"Status": "sent",
"Direction": "outbound"
}
]
}
]
A side note:
If on different documents (or on different Sessions) the Sessions.Id: "Session01" condition is verified ("Session01"is not an unique key), the document's Messages that match the other conditions should also be added.
The result output doesn't mention neither the document or Sessions levels.
Second query — update
I want to update the Sessions.Conversations.Messages.Status of all those messages (same condition as before) to "read".
The collection should have now the following documents:
Please note the changes on:
Sessions.Conversations.Messages.Id = "Message03"
Sessions.Conversations.Messages.Id = "Message04"
Sessions.Conversations.Messages.Id = "Message05"
at Sessions.Id = "Session01"
[
{
"Id": "OP01",
"Sessions": [
{
"Id": "Session01",
"Conversations": [
{
"Id": "Conversation01",
"Messages": [
{
"Id": "Message01",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message02",
"Status": "delivered",
"Direction": "internal"
},
{
"Id": "Message03",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message04",
"Status": "read",
"Direction": "outbound"
}
]
},
{
"Id": "Conversation02",
"Messages": [
{
"Id": "Message05",
"Status": "read",
"Direction": "outbound"
}
]
}
]
},
{
"Id": "Session02",
"Conversations": [
{
"Id": "Conversation03",
"Messages": [
{
"Id": "Message06",
"Status": "read",
"Direction": "inbound"
},
{
"Id": "Message07",
"Status": "delivered",
"Direction": "internal"
}
]
},
{
"Id": "Conversation04",
"Messages": []
}
]
}
]
},
{
"Id": "OP02",
"Sessions": [
{
"Id": "Session03",
"Conversations": []
}
]
},
{
"Id": "OP03",
"Sessions": []
}
]
How can I accomplish these results with an aggregate and update_one queries?
Here comes a visual explanation of both queries:

I have written the aggregation query
db.session.aggregate([
{
$unwind:"$Sessions"
},
{
$unwind:"$Sessions.Conversations"
},
{
$unwind:"$Sessions.Conversations.Messages"
},
{
$match:{
"Sessions.Id" : "Session01",
"Sessions.Conversations.Messages.Direction":{
$in:[
"inbound", "outbound"
]
},
"Sessions.Conversations.Messages.Status":{
$in:[
"sent", "delivered"
]
}
}
},
{
$group:{
"_id":"$Sessions.Conversations.Id",
"Messages":{
$push:"$Sessions.Conversations.Messages"
}
}
}
]).pretty()
Output
{
"_id" : "Conversation02",
"Messages" : [
{
"Id" : "Message05",
"Status" : "sent",
"Direction" : "outbound"
}
]
}
{
"_id" : "Conversation01",
"Messages" : [
{
"Id" : "Message03",
"Status" : "delivered",
"Direction" : "inbound"
},
{
"Id" : "Message04",
"Status" : "sent",
"Direction" : "outbound"
}
]
}
Now to Update the document:
I have used the positional-filters
db.session.update(
{},
{
$set:{
"Sessions.$[session].Conversations.$[].Messages.$[message].Status":"read"
}
},
{
"arrayFilters": [{"session.Id":"Session01"},{ "message.Id": "Message05" }]
}
)
This will update the status as read for "session.Id":"Session01" and "message.Id": "Message05"
Hope this will help you. :)
UPDATE
db.session.update(
{},
{
$set:{
"Sessions.$[session].Conversations.$[].Messages.$[message].Status":"read"
}
},
{
"arrayFilters": [
{
"session.Id":"Session01"
},
{
"message.Direction": {
$in :[
"inbound",
"outbound"
]
},
"message.Status": {
$in :[
"sent",
"delivered"
]
}
}
]
}
)

Related

Matching a value in subdocument array and matching array to _id

I have a collection of Users which looks like this:
{
"_id": {
"$oid": "5ff6298f2056ad02272d10f2"
},
"name": "Billy Bob",
"story": {
"posts": [
{"id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"},
{"id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"},
{"id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"}
],
"whoCanView": [{
"$oid": "5ff891bd749ed24316272317"
}, {
"$oid": "5ffc89392056ad02272d10f3"
}]
}
}
I want to get all users and their stories listed in an array which I call "contacts". The user currently querying the collection may only get results if their userId is in the "whoCanView" array.
This is what i have so far but doesn't seem to be working:
users.find({'story' : {
'$elementMatch' : {
'_id' : {'$in' : contacts},
ObjectID(userId) : {'$in' : 'story.whoCanView'}
}
}
});
Any guidance would be appreciated. Thanks!
ObjectID(userId) : {'$in' : 'story.whoCanView'}
Above line along with the usage of $elemMatch operator is wrong in your code. Try this query instead:
let contacts = [
ObjectId("5ff6298f2056ad02272d10f2"),
ObjectId("5ff6298f2056ad02272d10f3"),
ObjectId("5ff6298f2056ad02272d10f4")
];
let userId = ObjectId("5ff891bd749ed24316272317");
db.users.find(
{
"_id": { $in: contacts },
"story.whoCanView": userId
},
{
"strories": "$story.posts"
}
);
Output when userId is ObjectId("5ff891bd749ed24316272317"):
/* 1 createdAt:1/7/2021, 2:50:15 AM*/
{
"_id" : ObjectId("5ff6298f2056ad02272d10f2"),
"strories" : [
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
}
]
},
/* 2 createdAt:1/7/2021, 2:50:15 AM*/
{
"_id" : ObjectId("5ff6298f2056ad02272d10f3"),
"strories" : [
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
}
]
},
/* 3 createdAt:1/7/2021, 2:50:15 AM*/
{
"_id" : ObjectId("5ff6298f2056ad02272d10f4"),
"strories" : [
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
},
{
"id" : "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45"
}
]
}
Output when userId is ObjectId("5ff891bd749ed24316272318"):
[]
Test data in users collection:
[
{
"_id": ObjectId("5ff6298f2056ad02272d10f2"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
},
{
"_id": ObjectId("5ff6298f2056ad02272d10f3"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
},
{
"_id": ObjectId("5ff6298f2056ad02272d10f4"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
},
{
"_id": ObjectId("5ff6298f2056ad02272d10f5"),
"name": "Billy Bob",
"story": {
"posts": [
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" },
{ "id": "b4a12557-4dfa-47eb-9f27-7bbcad0f4c45" }
],
"whoCanView": [
ObjectId("5ff891bd749ed24316272317"),
ObjectId("5ffc89392056ad02272d10f3")
]
}
}
]

JSON conversion using JOLT

I am trying to convert a JSON to different format using JOLT (using NiFi JoltTransformJson processor). For single JSON record, the JOLT am using is working fine in JOLT app demo whereas if i execute with multiple JSON records then I am not getting expected output in JOLT app demo. Could anyone correct me what additional changes I need to do in JOLT spec to handle multiple JSON records.
sample input json
[
{
"pool": {
"field": [
{
"name": "BillingDay",
"value": "12"
},
{
"name": "Custom1",
"value": "POOL_BASE_PLAN_3GB"
}
]
},
"usage": {
"version": "3",
"quota": {
"name": "POOL_TOP_UP_1GB_2",
"cid": "5764888998010953848"
}
},
"state": {
"version": "1",
"property": [
{
"name": "SMS_RO_TOP",
"value": "1"
},
{
"name": "BillingTimeStamp",
"value": "2020-06-12T01:00:05"
},
{
"name": "timereset",
"value": "2020-01-12T00:35:53"
}
]
}
},
{
"pool": {
"field": [
{
"name": "PoolID",
"value": "111100110000003505209"
},
{
"name": "BillingDay",
"value": "9"
}
]
},
"usage": {
"version": "3"
},
"state": {
"version": "1",
"property": [
{
"name": "BillingTimeStamp",
"value": "2020-06-09T01:00:05"
},
{
"name": "timereset",
"value": "2019-03-20T17:10:38"
}
]
}
}
]
JOLT using:
[
{
"operation": "modify-default-beta",
"spec": {
"state": {
"property": {
"name": "NOTAVAILABLE"
}
},
"usage": {
"quota": {
"name": "NOTAVAILABLE"
}
}
}
},
{
"operation": "shift",
"spec": {
"pool": {
"field": {
"*": {
"value": "pool_item.#(1,name)"
}
}
},
// remaining elements print as it is
"*": "&"
}
}
]
Expected output JSON:
[
{
"pool_item" : {
"BillingDay" : "12",
"Custom1" : "POOL_BASE_PLAN_3GB"
},
"usage" : {
"version" : "3",
"quota" : {
"name" : "POOL_TOP_UP_1GB_2",
"cid" : "5764888998010953848"
}
},
"state" : {
"version" : "1",
"property" : [ {
"name" : "SMS_RO_TOP",
"value" : "1"
}, {
"name" : "BillingTimeStamp",
"value" : "2020-06-12T01:00:05"
}, {
"name" : "timereset",
"value" : "2020-01-12T00:35:53"
} ]
}
},
{
"pool_item" : {
"BillingDay" : "9",
"PoolID" : "111100110000003505209"
},
"usage" : {
"version" : "3",
"quota" : {
"name" : "NOTAVAILABLE"
}
},
"state" : {
"version" : "1",
"property" : [ {
"name" : "SMS_RO_TOP",
"value" : "1"
}, {
"name" : "BillingTimeStamp",
"value" : "2020-06-12T01:00:05"
}, {
"name" : "timereset",
"value" : "2020-01-12T00:35:53"
} ]
}
}
]
This below jolt shift specification will work for your multiple json's in input array.
[
{
"operation": "shift",
"spec": {
"*": {
"pool": {
"field": {
"*": {
"value": "[&4].pool_item.#(1,name)"
}
}
},
"usage": "[&1].usage",
"state": "[&1].state"
}
}
}
]

Filter by nested arrays/objects values (on different levels) and $project by matching groups (on a specific level) - MongoDB Aggregate

Considering I have the following collection (ignoring documents _id):
Collection
[
{
"Id": "1",
"Level2": [
{
"Id": "1.1",
"Level3": [
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
},
{
"Id": "1.1.2.2",
"Status": "c"
}
]
},
{
"Id": "1.1.3",
"Level4": [
{
"Id": "1.1.3.1",
"Status": "c"
}
]
}
]
}
]
},
{
"Id": "2",
"Level2": [
{
"Id": "2.1",
"Level3": [
{
"Id": "2.1.1",
"Level4": [
{
"Id": "2.1.1.1",
"Status": "a"
}
]
}
]
}
]
}
]
Conditions
I want to query it such as:
Level2.Id: "1.1"
Level2.Level3.Level4.Status $in ["a", "b"]
The $project should:
Group Level2.Level3.Level4 matches by Level2.Level3
Should not include Level2.Level3 if it is an empty array
The expected result should be:
Result 1
[
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
}
]
}
]
Please notice that if current Level2.Id: "2.1" has the value "1.1" instead, the result should be (matches on different documents):
Result 2
[
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
}
]
},
{
"Id": "2.1.1",
"Level4": [
{
"Id": "2.1.1.1",
"Status": "a"
}
]
}
]
How can I accomplish this with an aggregate query?
( filtering different levels and grouping by some level )
Here it is the answer (thank you to #Vijay Rajpurohit for his help on this):
db.collection.aggregate([
{
$unwind: "$Level2"
},
{
$unwind: "$Level2.Level3"
},
{
$unwind: "$Level2.Level3.Level4"
},
{
$match: {
"Level2.Id": "1.1",
"Level2.Level3.Level4.Status": {
$in: [
"a",
"b"
]
}
}
},
{
$group: {
"_id": "$Level2.Level3.Id",
"Messages": {
$push: "$Level2.Level3.Level4"
}
}
}
])
The result of this query is:
[
{
"Messages": [
{
"Id": "1.1.2.1",
"Status": "a"
}
],
"_id": "1.1.2"
},
{
"Messages": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
],
"_id": "1.1.1"
}
]
Mongo Playground

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.

Mongo returning an array element

I have the following JSON document in my mongoDB which I added with mingoimport.
I am trying to return a single element from the questions array where theQuestion equals "q1".
{
"questions": [
{
"questionEntry": {
"id": 1,
"info": {
"seasonNumber": 1,
"episodeNumber": 1,
"episodeName": "Days Gone Bye"
},
"questionItem": {
"theQuestion": "q1",
"attachedElement": {
"type": 1,
"value": ""
}
},
"options": [
{
"type": 1,
"value": "o1"
},
{
"type": 1,
"value": "o1"
}
],
"answer": {
"questionId": 1,
"answer": 1
},
"metaTags": [
"Season 1",
"Episode 1",
"Rick Grimmes"
]
}
},
{
"questionEntry": {
"id": 1,
"info": {
"seasonNumber": 1,
"episodeNumber": 1,
"episodeName": "Days Gone Bye"
},
"questionItem": {
"theQuestion": "q2",
"attachedElement": {
"type": 1,
"value": ""
}
},
"options": [
{
"type": 1,
"value": "o2"
},
{
"type": 1,
"value": "o2"
}
],
"answer": {
"questionId": 1,
"answer": 1
},
"metaTags": [
"Season 1",
"Episode 1",
"Rick Grimmes",
"Glenn Rhee"
]
}
}
]
}
I ran the query db.questions.find({"questions.questionEntry.questionItem.theQuestion" : "q1"}) but this retruned the whole document (both questionEntry's in question array!
I have tried db.questions.find({"questions.questionEntry.questionItem.theQuestion" : "q1"}, _id:0," questions.questionItem": {$elemMatch : {theQuestion: "q1"}}})
But get the following error:
Error: error: {
"$err" : "Can't canonicalize query: BadValue Cannot use $elemMatch projection on a nested field.", "code" : 17287
Is there a way I could limit the result to just the array element which contains it?
Thanks
db.questions.find({},{"questions.questionEntry.questionItem.theQuestion" : "q1"});
or
db.questions.find({"questions.questionEntry.questionItem.theQuestion" : "q1"},{'questions.$':1});
please try these.
If you want to use $elemMatch the query should be:
db.questions.find(
{"questions.questionEntry.questionItem.theQuestion" : "q1"},
{
'_id':0,
"questions": {
$elemMatch : {"questionEntry.questionItem.theQuestion": "q1"}
}
}
)