I wannt to access the fields in my JSONArray. The nested brackets inside the JSONArray is very troublesome. I can't see how this format is an acceptable JSONArray return value. I get an JSONException when I try to access a field (eg "rethink3__Address__c") using getJSONObject().
[
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
],
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
]
]
A [] = json array and {} = json object. So try this.
let myArray = [
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
],
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
]
];
myArray.forEach((myNestedArray)=>{
let obj = myNestedArray[0]
console.log(obj.attributes.type);
console.log(obj._soupLastModifiedDate);
})
Related
I need to use integer numbers for my array object ID. now in mongoDB the default _id for array objects are ObjectId('62e10eb9150cd328edd6f6y3') and i need to change that to integer numbers. Can I know is that possible or not. If that possible please mention how to do that.
now in mongoDB the default _id for array objects are ObjectId('62e10eb9150cd328edd6f6y3')
This is not true, this is true for mongoose as it generates an ObjectId for each object by default - but this is not a MongoDB feature.
mongoose does not offer any alternatives as a feature, in your case using index of array as _id, but you can work around it by executing a slightly less efficient update using the aggregation pipeline syntax, like so:
const newArrayItem = { ... new array items };
db.collection.updateOne(
{},
[
{
"$set": {
"items": {
$concatArrays: [
{
$ifNull: [
"$items",
[]
]
},
[
{
$mergeObjects: [
newArrayItem,
{
_id: {
$size: {
$ifNull: [
"$items",
[]
]
}
}
}
]
}
]
]
}
}
}
])
Mongo Playground
My input:
[
{
"nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/4e0becf9-c3ec-4002-a32b-2e35b76469b2",
"subscrCond": {
"serviceName": "namf-evts"
},
"subscriptionId": "36bc52dfdbdd4044b97ef15684706205",
"validityTime": "2022-04-30T16:40:48.274Z",
"reqNotifEvents": [
"NF_DEREGISTERED",
"NF_PROFILE_CHANGED",
"NF_REGISTERED"
]
},
{
"nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/5319def1-af0b-4b7b-a94e-b787e614c065",
"subscrCond": {
"serviceName": "nbsf-management"
},
"subscriptionId": "e2e904bb52ca4fd6b048841c83a4c38e",
"validityTime": "2022-04-30T16:40:48.26Z",
"reqNotifEvents": [
"NF_DEREGISTERED",
"NF_PROFILE_CHANGED",
"NF_REGISTERED"
]
},
{
"nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/31dfe10b-4020-47bd-943e-a3e293086b29",
"subscrCond": {
"serviceName": "namf-comm"
},
"subscriptionId": "e508077fab4f4b8d9dd732176a3777b9",
"validityTime": "2022-04-30T16:40:48.273Z",
"reqNotifEvents": [
"NF_DEREGISTERED",
"NF_PROFILE_CHANGED",
"NF_REGISTERED"
]
}
]
I would like to sort it by "subscriptionId" and "serviceName".
I can sort by subscriptionId but I don't know how to specify serviceName to the following expression.
jq -S '.|=sort_by(.subscriptionId)|.[].reqNotifEvents|=sort |del(.[].subscriptionId, .[].validityTime, .[].nfStatusNotificationUri)'
You can parameterize sort_by by a list of keys like so:
sort_by(.subscriptionId, .subscrCond.serviceName)
Online demo
I want to update this document in MongoDB
[
{
"persons" : [
{
"name":"Javier",
"occupation":"teacher"
},
{
"name":"Juliana",
"occupation":"novelist"
},
{
"name":"Henry",
"occupation":"plumber"
}
]
}
]
let's say I have a global array variable that contains this
var peopleGlobalVar = [
{
"name":"Javier",
"occupation":"gardener"
},
{
"name":"Henry",
"occupation":"postman"
}
]
I want to update the document with the value in globalVar WITHOUT common javascript looping, so if the names match, the occupation value will change, and I'm looking for the "pure query" solution.
the code I expect:
collection.update(
{},
{
$set: {
// code ...
}
},
{
$arrayFilter: [
{
"elem.name": {
$in:
$function : {
body: function(updatedPeopleFromGlobalVariable){
let names = [];
updatedPeopleFromGlobalVariable.forEach(function(person){
names.push(person.name);
return names;
},
args: peopleGlobalVar,
lang:"js",
}
},
"multi": true
}
]
}
)
What the output should be
[
{
"persons" : [
{
"name":"Javier",
"occupation":"gardener"
},
{
"name":"Juliana",
"occupation":"novelist"
},
{
"name":"Henry",
"occupation":"postman"
}
]
}
]
Anyone have the solution to this?
I am trying to query documents from a mongodb collection, based on array of input query parameters sent from URL.
Sample Database Data
[
{
"drawings": {
"circle": [],
"square": [
{
"id": "828",
"name": "square"
}
],
"cube": []
},
{
"drawings": {
"circle": [
{
"id": "827",
"name": "circle"
}
],
"square": [],
"cube": []
},
{
"drawings": {
"circle": [],
"square": [],
"cube": [
{
"id": "829",
"name": "cube"
}
]
}
]
Input Query Parameter:
query = ["square","cube"];
Expected Output:
[
{
"drawings": {
"circle": [],
"square": [
{
"id": "828",
"name": "square"
}
],
"cube": []
},
{
"drawings": {
"circle": [],
"square": [],
"cube": [
{
"id": "829",
"name": "cube"
}
]
}
]
Best suited Mongoose Query:
Schema.find({
$or:[
{'drawings.square':{$elemMatch:{ name:'square'}}},
{'drawings.cube':{$elemMatch:{ name:'cube'}}}
]
});
Tried Below method. But, it is not correct.
let draw = ["square","cube"];
let draw_query =[];
for (let a=0; a<draw.length;a++){
draw_query.push("{\"drawings."+ draw[a] +"\':{$elemMatch:{ name:\"" + draw[a] + "\"}}}");
}
It creates array with single quoted strings. It cannot be used.
[ '{"drawings.square":{$elemMatch:{ name:"square"}}}',
'{"drawings.cube":{$elemMatch:{ name:"cube"}}}' ]
How to generate this mongoose query dynamically? or is there any better mongoose query to achieve the expected result.
You can query it directly using dot notation so the query should look like below:
db.collection.find({
$or: [
{
"drawings.square.name": "square"
},
{
"drawings.circle.name": "circle"
}
]
})
You can build it in JS using .map(), try:
var query = ["square","cube"];
var orQuery = { $or: query.map(x => ({ [x + ".name"]: x }) ) }
I have the following code:
JSONArray array = new JSONArray();
array.put(allWaitClasses.get(0).allPairs.get(0).pair);
array.put(allWaitClasses.get(0).allPairs.get(1).pair);
array.put(allWaitClasses.get(0).allPairs.get(2).pair);
array.put(allWaitClasses.get(0).allPairs.get(3).pair);
array.put(allWaitClasses.get(0).allPairs.get(4).pair);
array.put(allWaitClasses.get(0).allPairs.get(5).pair);
json = array.toString();
What I get is the following:
[
{
"name": "User I/O"
},
{
"key": "61410583140000"
},
...
]
But what I want is squared brackets:
var data =[
[
1229904000000,
12.74
],
[
1229990400000,
115.20
],
...
Actually I want to follow the template of this API:
https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?
So how can I get the squared brackets insteand of the curved ones?
you should iterate over data and push items in following way :
var arrayX = [];
$.forEach( data, function ( item ) {
arrayX .push( [
item.timestamp, item.value
] );
} )