APEX_JSON.get_count not recognizing JSON path - arrays

We are on Oracle 11g, no chance of upgrade at this time.
This JSON comes from an outside source, no chance of reformat.
Is this JSON the problem? I am able to run htis code using simple JSON.
APEX_JSON.get_count only seems to recognize the path hits.hits
I need to create an inner loop to parse full_na
declare
l_cnt PLS_INTEGER ;
l_cnt_2 PLS_INTEGER ;
json varchar2 (32767)
:= '{
"took" : 45,
"timed_out" : false,
"_shards" : {
"total" : 102,
"successful" : 102,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.0,
"hits" : [
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "pty990183115",
"_score" : 0.0,
"_routing" : "16",
"_source" : {
"pty_id" : 990183115,
"asgmt_typ_cde" : null,
"country_code" : null,
"decd_ind" : "N",
"pay_dir_ind" : "N",
"paybl_ind" : null,
"paybl_soc_cde" : null,
"paybl_soc_pty_id" : null,
"pty_typ_cde" : "NFMBR",
"succr_act_fl" : null,
"txfr_pty_id" : null,
"del_fl" : "N",
"ipi_bas_nr" : "I-001775403-7",
"pty_rol_typ_cde" : "W",
"document_type" : "entity",
"entity_type" : "pty",
"my_join_field" : "entity"
},
"inner_hits" : {
"name" : {
"hits" : {
"total" : 3,
"max_score" : 10.105857,
"hits" : [
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "name11627766990183115",
"_score" : 10.105857,
"_routing" : "16",
"_source" : {
"pty_na_id" : 1.1627766E7,
"del_fl" : "N",
"suf" : null,
"pty_id" : 990183115,
"na" : "LAMBERT",
"fst_na" : "MIRANDA",
"sal" : null,
"mid_init" : null,
"pty_na_typ_cde" : "PP",
"ipi_na_nr" : 4.00943E8,
"full_na" : "LAMBERT MIRANDA",
"suf_oth" : null,
"my_join_field" : {
"name" : "name",
"parent" : "pty990183115"
},
"document_type" : "name"
}
},
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "name11627765990183115",
"_score" : 8.721075,
"_routing" : "16",
"_source" : {
"pty_na_id" : 1.1627765E7,
"del_fl" : "N",
"suf" : null,
"pty_id" : 990183115,
"na" : "LAMBERT",
"fst_na" : "MIRANDA",
"sal" : null,
"mid_init" : "L",
"pty_na_typ_cde" : "PP",
"ipi_na_nr" : 4.00942985E8,
"full_na" : "LAMBERT MIRANDA L",
"suf_oth" : null,
"my_join_field" : {
"name" : "name",
"parent" : "pty990183115"
},
"document_type" : "name"
}
},
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "name11627764990183115",
"_score" : 8.721075,
"_routing" : "16",
"_source" : {
"pty_na_id" : 1.1627764E7,
"del_fl" : "N",
"suf" : null,
"pty_id" : 990183115,
"na" : "LAMBERT",
"fst_na" : "MIRANDA",
"sal" : null,
"mid_init" : "LEIGH",
"pty_na_typ_cde" : "PA",
"ipi_na_nr" : 4.00942887E8,
"full_na" : "LAMBERT MIRANDA LEIGH",
"suf_oth" : null,
"my_join_field" : {
"name" : "name",
"parent" : "pty990183115"
},
"document_type" : "name"
}
}
]
}
}
}
}
]
}
}';
begin
apex_json.parse (json);
l_cnt := APEX_JSON.get_count(p_path => 'hits.hits');
DBMS_OUTPUT.put_line('hits.hits Count : ' || l_cnt);
l_cnt_2 := APEX_JSON.get_count(p_path => 'hits.hits.inner_hits.name.hits.hits');
DBMS_OUTPUT.put_line('inner before loop Count : ' || l_cnt_2);
FOR i IN 1 .. l_cnt LOOP
DBMS_OUTPUT.put_line('Counter : ' || i);
DBMS_OUTPUT.put_line('pty_id : ' ||
APEX_JSON.get_varchar2(p_path => 'hits.hits[%d]._source.pty_id', p0 => i));
l_cnt_2 := APEX_JSON.get_count(p_path => 'hits.hits[%d].inner_hits.name.hits.hits');
DBMS_OUTPUT.put_line('inner after loop Count : ' || l_cnt_2);
end loop;
end;

Related

json contain array how to populate it in thyemleaf (how can i get carv value in thyemleaf)

{
"_id" : ObjectId("6128c6c54859a6c1579f98ad"),
"classByIngredient" : "",
"cuisineStyle" : "Indian - North",
"dietPreference" : "Vegetarian",
"dishClass" : "Staple",
"dishName" : "Aloo Gobi Paratha",
"dishVariation" : "Aloo Gobi Paratha with ghee",
"dishNameLower" : "aloo gobi paratha",
"dishVariationLower" : "aloo gobi paratha with ghee",
"sizes" : [
{
"name" : "Piece",
"calories" : {
"value" : "164.0",
"unit" : "kcal"
},
"carbs" : {
"value" : "29.0",
"unit" : "gms"
},
"fat" : {
"value" : "2.9",
"unit" : "gms"
},
"fibre" : {
"value" : "0",
"unit" : "gms"
},
"iron" : {
"value" : "0.0",
"unit" : "mg"
},
"protein" : {
"value" : "4.6",
"unit" : "gms"
},
"rawWeight" : "",
"sodium" : {
"value" : "0",
"unit" : "mg"
},
"sugars" : {
"value" : "0",
"unit" : "gms"
},
"calcium" : {
"value" : "0.0",
"unit" : "mg"
},
"cookedWeight" : "100 gms",
"measurement" : "https://smit-diet-measurement.s3.amazonaws.com/Piece.png",
"type" : "ingredient"
}
],
"enable" : "true",
"householdMeasure" : "Piece",
"createdAt" : ISODate("2021-08-27T11:04:36.212Z"),
"verified" : true,
"updatedAt" : ISODate("2021-08-27T11:04:36.212Z"),
"_class" : "com.diet.library.dto.DietLibrary"
}

Adding data into embedded array - The field must be an array but is of type object in document

I am trying to add array to an existing array in a document but get the error :
'The field 'pnrs' must be an array but is of type object in document'.
Not sure I understand what I'm doing wrong...
I have this structure in my collection :
{
"flightName" : "BA1304:2020-05-25",
"flightInfo" : {
"flightNumber" : "BA1304",
"flightDate" : "2020-05-25",
"airline" : "British Airways",
"departCode" : "LHR",
"arriveCode" : "ABZ"
},
"pnrs" :
{
"bookingReference" : "MGE60M",
"passengers" :
{
"passportNumber" : "12345678",
"issueCountry" : "GBR",
"issueDate" : "2020-05-01",
"expiryDate" : "2020-05-01",
"emailAddress" : "johndoe#email.com",
"name" : {
"firstName" : "Jon",
"surname" : "Doe",
"dob" : "1980-05-07"
},
"address" : {
"address1" : "31 High Street",
"address2" : "",
"city" : "London",
"code" : "SE10 8BD",
"country" : "United Kingdom"
},
"creditCard" : {
"creditCardNumber" : "4929109468047005",
"cardType" : "creditCard",
"ccExpiryDate" : "2021-05-01",
"ccIssueDate" : "2018-04-01"
}
}
}
}
and I want to add another array in 'pnrs' :
{
"bookingReference" : "XB6RD9",
"passengers" :
{
"passportNumber" : "98658421",
"issueCountry" : "GBR",
"issueDate" : "20217-08-01",
"expiryDate" : "2027-08-01",
"emailAddress" : "bill.smith#someemail.com",
"name" : {
"firstName" : "Bill",
"surname" : "Smith",
"dob" : "1962-01-14"
},
"address" : {
"address1" : "22 Bridge Street",
"address2" : "",
"city" : "Glasgow",
"code" : "G3 6EF",
"country" : "United Kingdom"
},
"creditCard" : {
"creditCardNumber" : "432187651234",
"cardType" : "creditCard",
"ccExpiryDate" : "2022-02-01",
"ccIssueDate" : "2020-01--01"
}
}
}
I have tried :
db.collection.update( { flightName: "BA1304:2020-05-25" },
{$push:
{pnrs:
{
"bookingReference" : "XB6RD9",
"passengers" :
{
"passportNumber" : "98658421",
"issueCountry" : "GBR",
"issueDate" : "20217-08-01",
"expiryDate" : "2027-08-01",
"emailAddress" : "bill.smith#someemail.com",
"name" : {
"firstName" : "Bill",
"surname" : "Smith",
"dob" : "1962-01-14"
},
"address" : {
"address1" : "22 Bridge Street",
"address2" : "",
"city" : "Glasgow",
"code" : "G3 6EF",
"country" : "United Kingdom"
},
"creditCard" : {
"creditCardNumber" : "432187651234",
"cardType" : "creditCard",
"ccExpiryDate" : "2022-02-01",
"ccIssueDate" : "2020-01--01"
}
}
}
}
}
)
But I get 'The field 'pnrs' must be an array but is of type object in document'

mongodb ObjectId find with sails, returns all the data

in my sails controller I have implement a function to find users. but when I try to search using ObjectId it returns all the data. I couldn't find any reason. but it works when I create a new separate field as "userID", and find using "userID". I think the problem is find using the "ObjectId" what could be the issue.
module.exports = {
updateOrders : function(req,res){
var ObjectId = require('mongodb').ObjectID;
console.log("req " + req);
var data = req.body;
var obj = [];
console.log("\ndata "+(data));
var jdata = JSON.stringify(data);
console.log("\njdata: "+jdata);
data.forEach(function(orders){
var objid = ObjectId(orders.id);
console.log("Obj orders id :" + objid);
ApplicationOrder.find({id: ObjectId(orders.id)}).exec(function (err, order) {
if (err){
return done(err);
}
console.log('order ' + order);
obj.push(order);
});
})
res.send(obj);
}
};
Json data: printed in the console.
{
"_id" : ObjectId("59a8f820caf1cae37af72c0c"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b70",
"name" : "Short x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "short_x.png"
}
],
"totalQty" : 218
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan#yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:12.584Z"),
"updatedAt" : ISODate("2017-09-01T06:03:12.584Z")
}
{
"_id" : ObjectId("59a8f82fcaf1cae37af72c0d"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b6d",
"name" : "Shirt x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "shirt_x.png"
}
],
"totalQty" : 244
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan#yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:27.022Z"),
"updatedAt" : ISODate("2017-09-01T06:03:27.022Z")
}
{
"_id" : ObjectId("59a8f83ecaf1cae37af72c0e"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b71",
"name" : "Beach Suit",
"qty" : 2,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "cloth3.png"
}
],
"totalQty" : 238
}
],
"amount" : "500",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan#yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:42.439Z"),
"updatedAt" : ISODate("2017-09-01T06:03:42.439Z")
}
For standard sails.js/waterline queries, you have to use the id as a String in your query params, do not use ObjectId(ordersId).
When you use a sails native query, use ObjectId(ordersId).
If you are using any mongodb adapter then do not try to convert id by ObjectId(orders.id).Try normal query like
ApplicationOrder.find({'id':orders.id})
For native sails query,you have to convert id to objectId.
I finally was able to fix the issue. I had to edit the my model class in sails and use only orders.id model class,
_id:{type : 'objectid'} and when search
.find({_id: orders.id})
this worked for me perfectly.

unable to use $pullAll

I have a collection which contains the documents as shown below
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(929),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8c0")
},
{
"plateNumber" : "ADFN3R4",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "jack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bf")
},
{
"plateNumber" : "ADFN3R8",
"direction" : "EXIT",
"routeNumber" : NumberInt(500),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8be")
},
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
},
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "sandesh",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bc")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
where busEntryExitEvent is a array i am trying to remove the array elements where direction is exit across all the documents in the collection
result should contain all the documents with direction entry
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
i am trying to do like this
db.busEntryExitDoc.update(
{ $pull: { busEntryExitEvent: { "direction" : "EXIT"} } },
{ multi: false }
)
this has to be done to all the documents there is no where condition here how to do it please help
The MongoDB update() methods expects three arguments: db.collection.update(query, update, options). If you want to perform the update on all documents, just use an empty object as query:
db.busEntryExitDoc.update({},
{ $pull: { 'busEntryExitInformation.busEntryExitEvent' : {'direction' : "EXIT"} } },
{ multi: true }
);
Also, note the use of the dot notation to access the array field: you have to point the $pull operator to an array otherwise the operation fails. Finally, use { multi: true } as options since you want to update all the documents in your collection.
You are doing it wrong the first argument to .update() is the "query" argument which should be an empty document here.
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
{ multi: true }
)
The same thing apply to the .updateMany() method
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
)

How to find all in an array of array in MongoDB

Below is the schema for an array contacts. The contacts array has a field hashtag which is another array. How do I find all the contacts which have a particular hashtag? For example all contacts with hashtag "zxc"?
"contacts" : [
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "tell.fadgfdg#gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
"verified" : true,
"favorite" : true,
"linkedinUserName" : null,
"facebookUserName" : null,
"twitterUserName" : "IamlifePaul",
"count" : 2,
"relationshipStrength_updated" : 0,
"contactRelation" : {
"decisionmaker_influencer" : null,
"prospect_customer" : "prospect"
},
"source" : "abc",
"mobileNumber" : "3546789",
"skypeId" : "123",
"designation" : "test",
"companyName" : "Something",
"location" : "Hyderabad, Telangana, India",
"personName" : "Naveen Paul",
"personId" : "565022d7dbeaeb9e17fc7083",
"hashtag" : [
"latestTag",
"anotherTag",
"#hash",
"openLove",
"hellTwo",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
},
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "naveenpaul.fadgfdg#gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
"verified" : true,
"favorite" : true,
"linkedinUserName" : null,
"facebookUserName" : null,
"twitterUserName" : "IamlifePaul",
"count" : 2,
"relationshipStrength_updated" : 0,
"contactRelation" : {
"decisionmaker_influencer" : null,
"prospect_customer" : "prospect"
},
"source" : "abc",
"mobileNumber" : "3546789",
"skypeId" : "123",
"designation" : "test",
"companyName" : "Something",
"location" : "Hyderabad, Telangana, India",
"personName" : "Naveen Paul",
"personId" : "565022d7dbeaeb9e17fc7083",
"hashtag" : [
"latestTag",
"anotherTag",
"#hash",
"openLove",
"hellTwo",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
},
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "naveenpaul.fadgfdg#gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
"verified" : true,
"favorite" : true,
"linkedinUserName" : null,
"facebookUserName" : null,
"twitterUserName" : "IamlifePaul",
"count" : 2,
"relationshipStrength_updated" : 0,
"contactRelation" : {
"decisionmaker_influencer" : null,
"prospect_customer" : "prospect"
},
"source" : "abc",
"mobileNumber" : "3546789",
"skypeId" : "123",
"designation" : "test",
"companyName" : "Something",
"location" : "Hyderabad, Telangana, India",
"personName" : "Naveen Paul",
"personId" : "565022d7dbeaeb9e17fc7083",
"hashtag" : [
"polly",
"tagger",
"#hash",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
}
I did this query - db.myColl.find({"contacts.hashtag":"zxc"},{"contacts":{$elemMatch:{"hashtag":"zxc"}}}).pretty()
which returned only one contact. I need to get all contacts.
You can do it via aggregation framework:
1) Do an initial match to reduce the input set for aggregation
2) Unwind contacts field.
3) Filter out contacts with hashtag 'zxc'
4) Group by id and put contacts on "contacts" field.
db.getCollection('contacts').aggregate([
{$match: {"contacts.hashtag":"zxc"}},
{$unwind: "$contacts"},
{$match: {"contacts.hashtag":"zxc"}},
{$group: {_id:"$_id", "contacts": {$push:"$contacts"}}}
])
You can use
For multiple hashtag in single doc
db.myColl.find({"contacts.hashtag":{$in : ['zxc','anotherTag']}});
//will return all doc with any of the two hashtags.
For single hashtag
db.myColl.find({"contacts.hashtag":'zxc'});
//return all documents with hashtag 'zxc'
Please refer $in

Resources