how to remove null, {}, [] from json object in node js? - node.js

The json format is like that:
[
[
{},
{
"Country": "Japan",
"cityName": "tokyo",
"onto": [
{
"level1": "one",
"articles": [
null,
{
"id": "114506604",
"name": "bunya3",
"abc": [
{
"filename": "attachmentsfilename3",
"size": 3
}
],
"image": {}
}
]
}
]
}
],
[
{}
]
]
We can see few null, {} and [{}]. How can we remove it ? By the way I am using node js. I have tried by nnjson
nnjson.removeNull(obj_summary);
But not works object without key.

If we assume that your data is always going to be an array, we can map over it and remove empty arrays and objects from the first level:
const data = [
[
{},
{
Country: 'Japan',
cityName: 'tokyo',
onto: [
{
level1: 'one',
articles: [
null,
{
id: '114506604',
name: 'bunya3',
abc: [
{
filename: 'attachmentsfilename3',
size: 3
}
],
image: {}
}
]
}
]
}
],
[{}]
]
function clean(input) {
return input
.map(item => {
// remove empty arrays
if (Array.isArray(item) && item.length === 0) {
return null
}
// Remove empty objects
if (item instanceof Object && Object.keys(item).length === 0) {
return null
}
return item
})
.filter(item => item)
}
console.log(clean(data))

I found the solution.
To remove null I used:
let retSummary = JSON.parse(stringifySummary, (k, v) => Array.isArray(v) ?
v.filter(e => e !== null) : v);
To remove {} I used
var newArray = parObj.filter(value => Object.keys(value).length !== 0);

Related

Update an array element in MongoDB with NodeJS

**I want to update the "paymentStatus" field in the games array of "ballpool" or "valorant".I am using NodeJS.So please help me how can i update the payment status by giving the value "ballpool" or "valorant" as parameter **
{
"uniqueCode": "n5Eue",
"games": [
{
"ballpool": {
"email": "aniketshaw#gmail.com",
"name": "Aniket Shaw",
"phone": "31231231",
"ballpoolUID": "4232",
"paymentStatus": false,
"_id": "63de237567fa64e9711bb2b7",
"__v": 0
}
},
{
"valorant": {
"_id": "63de237567fa64e9711bb2b7",
"email": "aniketshaw#gmail.com",
"name": "Aniket Shaw",
"phone": "31231231",
"valorantUID": "4232",
"paymentStatus": true,
"__v": 0
}
}
],
}
Here's an example
let param = 'ballpool';
let setOperator;
let filterOperator;
if (param === 'ballpool') {
setOperator = {
'games.$[element].ballpool.paymentStatus': true,
};
filterOperator = {
"element.ballpool": { $ne: null }
};
} else if (param === 'valorant') {
setOperator = {
'games.$[element].valorant.paymentStatus': true,
};
filterOperator = {
"element.valorant": { $ne: null }
};
}
await Model.updateOne({
uniqueCode: 'foo',
}, {
$set: setOperator,
}, {
arrayFilters: [filterOperator]
});
Learn more about $[identifier] here

Remove Duplicate from Array of Object [duplicate]

This question already has answers here:
Group array items using object
(19 answers)
Closed 5 months ago.
Iam having an array of Object
[
{
REQUEST_TYPE: 3,
E_APN: [ 'internet' ],
value:0
},
{
REQUEST_TYPE: 3,
E_APN: [ 'internet' ],
value:0,
},
{
REQUEST_TYPE: 2,
E_APN: [ 'login' ]
value:0,
}
]
if REQUEST_TYPE of each object is same, then it should merge E_APN as shown below:
[
{
REQUEST_TYPE: 3,
E_APN: [ 'internet','internet' ],
value:0,
},
{
REQUEST_TYPE: 2,
E_APN: [ 'login' ],
value:0
}
]
In tried using filter
const resultArr = dataArr.filter((data,index)=>{
return dataArr.indexOf(data) === index;
})
but not able to push E_APN
var dup = [
{
REQUEST_TYPE: 3,
E_APN: ['internet'],
value: 0
},
{
REQUEST_TYPE: 3,
E_APN: ['internet'],
value: 0,
},
{
REQUEST_TYPE: 2,
E_APN: ['login'],
value: 0,
}
]
var unique = []
var ref = {}
var t = 0;
for (let i = 0; i < dup.length; i++) {
if (unique[ref[dup[i].REQUEST_TYPE]]) {
unique[ref[dup[i].REQUEST_TYPE]].E_APN = (unique[ref[dup[i].REQUEST_TYPE]].E_APN).concat(dup[i].E_APN)
} else {
ref[dup[i].REQUEST_TYPE] = t++;
unique.push(dup[i])
}
}
console.log(unique)

How can I create an Elasticsearch Query DSL for finding objects with empty array OR a value in same object

I am searching for objects in Elasticsearch 6.3.
I have 3 objects:
{
identifier: 01,
lineCodes: [],
},
{
identifier: 02,
lineCodes: [
{
link: "brussels",
name: "br"
},
{
link: "antwerp",
name: "an"
},
],
},
{
identifier: 03,
lineCodes: [
{
link: "ghent",
name: "gh"
},
],
}
My lineCodes schema is:
{
"lineCodes": {
"properties": {
"__typename": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"link": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
Can you help me with Query DSL's that find all objects with:
Brussels in lineCodes.link or empty lineCodes
All empty lineCodes
Ghent in lineCodes.link or empty lineCodes
query 1 should result in object 01 and 02
query 2 should result in object 01
query 3 should result in object 01 and 03
I tried this query, but it gives me not the empty LineCodes objects
let query = {
size: 1000,
index: 'catalog',
body: {
query: {
bool: {
must: [
{
bool: {
should: [
{ term: { 'lineCodes.link': lineCode } },
{ terms: { 'lineCodes': [] } },
],
},
}
],
}
}
}
};
You can use the exists query ( doc here ) and negate it with a must_not clause in a bool query
Could you try :
{
size: 1000,
index: 'catalog',
body: {
query: {
bool: {
must: [
{
bool: {
should: [
{ term: { 'lineCodes.link': lineCode } },
{
bool: {
must_not: {
exists: {
field: 'lineCodes'
}
}
}
}
],
},
}
],
}
}
}
}
NB: As you use a term query for linecode.link watch out for case and accents!
Hope it helps !

Find By ID and Remove From MongoDB array

My Collection:
geoGraphicalFilter: {
aCountries: [String],
aCities: [String],
aCoordinates: [{
coordinates: { type: Array }
}]
}
CollectionData
"geoGraphicalFilter": {
"aCoordinates": [
{
"_id": ObjectId("5acb641d93fa0e52557fc6aa"),
"coordinates": [
[
72.42919972527011,
23.0437703991947
],
[
72.45031407464302,
23.045823913521474
],
[
72.43263295281557,
23.030500782775746
],
[
72.42919972527011,
23.0437703991947
]
]
},
{
"_id": ObjectId("5acb641d93fa0e52557fc6ac"),
"coordinates": [
[
72.51520207511979,
23.038241551175616
],
[
72.55399754632015,
23.03934733892872
],
[
72.51812031852671,
23.025129376064214
],
[
72.51520207511979,
23.038241551175616
]
]
},
{
"_id": ObjectId("5acb641d93fa0e52557fc6ad"),
"coordinates": [
[
72.44653752434493,
23.02828905299478
],
[
72.4896245299627,
23.02828905299478
],
[
72.45477727044641,
23.0194417709901
],
[
72.44653752434493,
23.02828905299478
]
]
},
{
"_id": ObjectId("5acb641d93fa0e52557fc6ab"),
"coordinates": [
[
72.47451832878957,
23.045350028380867
],
[
72.50576069939376,
23.04835127278581
],
[
72.47949650871226,
23.031606634051897
],
[
72.47451832878957,
23.045350028380867
]
]
}
],
"aCities": [],
"aCountries": []
}
Remove From Database Snippet
const deleteZones = (req,res,next) => {
var body = _.pick(req.body, ["zones"]);
var zoneList = body.zones;
debug(zoneList)
var promise = function() {
return new Promise(function(resolve, reject) {
zoneList.forEach(itemA => {
console.log(itemA.coordinates)
huntingModel.update(
{ _id: req.body.id },
{ $pull: { 'geoGraphicalFilter.aCoordinates': itemA.id} },
(error, success) => {
if (error) console.log(error);
console.log(success);
}
);
});
resolve();
});
};
promise().then(function() {
return res.status(200).jsonp({
message: adminMessages.succ_zone_removed
});
});
}
Now the scenario is like when I am trying to delete data it shows success message but data does not get deleted.
var object = {
id:this.id,
zones: this.zonesDelete // Contains list of id
};
I am getting object in a requested body and I want to find the document from a collection and delete the particular array element by finding an id in geoGraphicalFilter.aCoordinates and wants to remove it.
As per documentation of $pull operator you can either specify a value or a condition
i.e.
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
In your scenario you need to either specify complete value of one or more aCoordinates item object or an condition that matches one or more aCoordinates item
Add the condition where you match id of aCoordinates item i.e.
Use following pull condition to solve the issue:
huntingModel.update(
{ _id: req.body.id },
{ $pull: { 'geoGraphicalFilter.aCoordinates': {'_id' : ObjectId(itemA.id)}} },
(error, success) => {
if (error) console.log(error);
console.log(success);
}
);

mongoose invalid operator $match

I am going to handle users search based on email_address, firstname, lastname, type and phone_number.
phone_number search will be exact search with & without country code while others will be containing & case-insensitive search.
So, I wrote the below code.
User.aggregate(
[
{
"$redact": {
"$cond": [
{
"$and": [
{
"$match": {
type: req.body.type,
email_address: new RegExp((req.body.email_address || req.body.any || '').toLowerCase(), "i"),
"firstname": new RegExp((req.body.firstname || req.body.any || '').toLowerCase(), "i") ,
"lastname": new RegExp((req.body.lastname || req.body.any || '').toLowerCase(), "i")
}
},
{
"$or": [
{
"$setIsSubset": [
[
{ "$substr": [ "$phone_number.local_number", 0, -1 ] }
],
[req.body.phone_number, req.body.any]
]
},
{
"$setIsSubset": [
[
{
"$concat": [
{ "$substr": [ "$phone_number.country_code", 0, -1 ] },
{ "$substr": [ "$phone_number.local_number", 0, -1 ] }
]
}
],
[req.body.phone_number, req.body.any]
]
},
{}
]
}
]
},
"$$KEEP",
"$$PRUNE"
]
}
}
],
function(err, users) {
if (err) {
return res.json({ success: false, err: err });
}
res.json({ success: true, users: users });
}
);
But when I run this code, I get "invalid operator '$match'" error.
If I remove $match, it evaluate req.body values as expression instead of value and emit "FieldPath 'abc' doesn't start with $" kind error.
So, I hope to get help how to solve this problem and search by conditions.
Please help me !!!
Move the $match outside $redact as it's an independent pipeline stage, it will provide the initial filter with the regex that can otherwise be invalid within the $redact pipeline:
User.aggregate([
{
"$match": {
"type": req.body.type,
"email_address": new RegExp((req.body.email_address || req.body.any || '').toLowerCase(), "i"),
"firstname": new RegExp((req.body.firstname || req.body.any || '').toLowerCase(), "i") ,
"lastname": new RegExp((req.body.lastname || req.body.any || '').toLowerCase(), "i")
}
},
{
"$redact": {
"$cond": [
{
"$or": [
{
"$setIsSubset": [
[ { "$substr": [ "$phone_number.local_number", 0, -1 ] } ],
[req.body.phone_number, req.body.any]
]
},
{
"$setIsSubset": [
[
{
"$concat": [
{ "$substr": [ "$phone_number.country_code", 0, -1 ] },
{ "$substr": [ "$phone_number.local_number", 0, -1 ] }
]
}
],
[req.body.phone_number, req.body.any]
]
}
]
},
"$$KEEP",
"$$PRUNE"
]
}
}
], function(err, users) {
if (err) {
return res.json({ success: false, err: err });
}
res.json({ success: true, users: users });
}
);

Resources