I have a top level JSON structure as follows:
{
"video": [],
"messages": [],
"notifications": []
}
and i have a database output (in variable "result") as follows i want to push into the "video" array:
[
{
"_id": "5f98ab906439155cfc6f9afb",
"status": "NOT_STARTED",
"date": "2020-10-27T23:21:52.683Z",
"callInvitees": [
{
"username": "user1"
},
{
"username": "user2"
}
]
},
{
"_id": "5f98aba0789e163e0c78908f",
"status": "NOT_STARTED",
"date": "2020-10-27T23:22:08.048Z",
"callInvitees": [
{
"username": "user1"
}
]
}
]
My code is:
let dashboardJSON = { "video": [], "messages": [], "notifications": [] };
dashboardJSON.video.push(result)
It works but i am ending up with too many arrays (i think) - it looks as follows:
{
"video": [
[
{
"_id": "5f98ab906439155cfc6f9afb",
"status": "NOT_STARTED",
"date": "2020-10-27T23:21:52.683Z",
"callInvitees": [
{
"username": "user1"
},
{
"username": "user2"
}
]
},
{
"_id": "5f98aba0789e163e0c78908f",
"status": "NOT_STARTED",
"date": "2020-10-27T23:22:08.048Z",
"callInvitees": [
{
"username": "user1"
}
]
}
]
],
"messages": [],
"notifications": []
}
I want "video": [ { ... }, { ... } ] whereas i have "video": [[ { ... }, { ... } ]]
How can i resolve this?
You can use the spread operator as follows:
let result = [
{
"_id": "5f98ab906439155cfc6f9afb",
"status": "NOT_STARTED",
"date": "2020-10-27T23:21:52.683Z",
"callInvitees": [
{
"username": "user1"
},
{
"username": "user2"
}
]
},
{
"_id": "5f98aba0789e163e0c78908f",
"status": "NOT_STARTED",
"date": "2020-10-27T23:22:08.048Z",
"callInvitees": [
{
"username": "user1"
}
]
}
]
let dashboardJSON = { "video": [], "messages": [], "notifications": [] };
dashboardJSON.video.push(...result)
console.log(dashboardJSON);
Just use Array.prototype.map() method. Map method returns a new array with the result by using the provided function.
const ret = {
video: [],
messages: [],
notifications: [],
};
const data = [
{
_id: '5f98ab906439155cfc6f9afb',
status: 'NOT_STARTED',
date: '2020-10-27T23:21:52.683Z',
callInvitees: [
{
username: 'user1',
},
{
username: 'user2',
},
],
},
{
_id: '5f98aba0789e163e0c78908f',
status: 'NOT_STARTED',
date: '2020-10-27T23:22:08.048Z',
callInvitees: [
{
username: 'user1',
},
],
},
];
ret.video = data.map((x) => x);
console.log(ret);
Related
hello everyone i have a document of materiels where each document has an array of articles and each article has an array of details here's my collection data:
{
"_id": "62f2404b42556d62e2939466",
"code": "120K",
"designation": "PIZZA",
"article": [
{
"etat": "Reçu",
"lot": "",
"marque": "Royal",
"fournisseur": "maatalah",
"dateachat": "2022-08-01T00:00:00.000Z",
"bc": null,
"bl": null,
"fc": null,
"quantite": 12,
"_id": "62f25d001a035d017369e1f8",
"detail": [
{
"serie": "12",
"ddp": "2023-01-01T00:00:00.000Z",
"_id": "62f3986fe462b8a173c7d220"
},
{
"serie": "13",
"ddp": "2023-03-01T00:00:00.000Z",
"_id": "62f39cc1862c1bf5bc40157a"
}
]
},
{
"etat": "Reçu",
"lot": "",
"marque": "margarita",
"fournisseur": "",
"dateachat": "2022-08-04T00:00:00.000Z",
"bc": null,
"bl": null,
"fc": null,
"quantite": 13,
"_id": "62f25d041a035d017369e1fb",
"detail": [
{
"serie": "12345",
"ddp": "2023-01-01T00:00:00.000Z",
"_id": "62f281158a159e976c7c68d5"
}
]
}
],
"qteglobal": 25,
"id": "62f2404b42556d62e2939466"
}
i want to remove an element from the detail array here my express js code:
const { id, idarticle, iddetail } = req.params;
const materiel = Materiel.findOneAndUpdate(
{ _id: id, "article._id": idarticle, "article.detail._id": iddetail },
{ $pull: { "article.$.detail": { _id: iddetail } } },
{ new: true }
);
there is no syntax error but the document it doesn't seem to be deleted.
Can anyone help me please ? thank you
Hello guys after trying some methods i end up with this solution:
const { id, idarticle, iddetail } = req.params;
await Materiel.findOneAndUpdate(
{
id,
"article._id": idarticle,
},
{
$pull: {
"article.$[].detail": {
_id: iddetail,
},
},
}
);
I have a JSON like this, how to get the value of StatusDescription? I tried many times but the result is undefined. Here is my JSON:
{
"meta": {
"a2": 200,
"ta": "dasd",
"asdd": "asdda"
},
"data": {
"items": [
{
"id": "",
"number": "",
"origin_info": {
"ItemReceived": "2021-10-02 02:07:49",
"phone": 123456789,
"trackinfo": [
{
"StatusDescription": "what i need",
"Details": "",
"substatus": "ok"
},
{
"StatusDescription": "what i need",
"Details": "",
"substatus": "ok"
}
]
},
"destination_info": null,
"lastEvent": "grgrgrgrgr",
"lastUpdateTime": "mewmemew"
}
]
}
}
I'm using in my NodeJS app, like myapp.js, and console.log()
Try this
I stored your sample json in variable json
var json = {
"meta": {
"a2": 200,
"ta": "dasd",
"asdd": "asdda"
},
"data": {
"items": [
{
"id": "",
"number": "",
"origin_info": {
"ItemReceived": "2021-10-02 02:07:49",
"phone": 123456789,
"trackinfo": [
{
"StatusDescription": "what i need",
"Details": "",
"substatus": "ok"
},
{
"StatusDescription": "what i need",
"Details": "",
"substatus": "ok"
}
]
},
"destination_info": null,
"lastEvent": "grgrgrgrgr",
"lastUpdateTime": "mewmemew"
}
]
}
}
Accessed it like below
console.log(json.data.items[0].origin_info.trackinfo[0].StatusDescription);
Items is an array and we took array element 0.
trackinfo again is an array and we took array element 0.
We can change array index or loop through and get required values.
You have to iterate through your items and trackinfo to get to StatusDescription. Try this one.
const data = {
"meta": {
"a2": 200,
"ta": "dasd",
"asdd": "asdda"
},
"data": {
"items": [
{
"id": "",
"number": "",
"origin_info": {
"ItemReceived": "2021-10-02 02:07:49",
"phone": 123456789,
"trackinfo": [
{
"StatusDescription": "what i need",
"Details": "",
"substatus": "ok"
},
{
"StatusDescription": "what i need",
"Details": "",
"substatus": "ok"
}
]
},
"destination_info": null,
"lastEvent": "grgrgrgrgr",
"lastUpdateTime": "mewmemew"
}
]
}
}
const items = data.data.items.map(item => item)
const trackinfo = items.map(item => item.origin_info.trackinfo).flat()
console.log(trackinfo)
const statusDescription = trackinfo.map(trackinfo => trackinfo.StatusDescription)
console.log(statusDescription)
I am tring to make a query where use the value and try to interpolate a string in a new field.
Mongo Database:
[
{
"state": "1",
"events": {
"1": [
{
"date": 123.2,
"msg": "msg1"
},
{
"date": 124.2,
"msg": "msg2"
}
],
"2": [
{
"date": 125.2,
"msg": "msg3"
},
{
"date": 126.2,
"msg": "msg4"
}
],
}
},
{
"state": "2",
"events": {
"1": [
{
"date": 123.2,
"msg": "msg1"
},
{
"date": 124.2,
"msg": "msg2"
}
],
"2": [
{
"date": 125.2,
"msg": "msg3"
},
{
"date": 126.2,
"msg": "msg4"
}
],
}
}
]
Aggregate query:
db.collection.aggregate({
"$match": {
"state": {
"$in": [
"1",
"2"
]
}
}
},
{
"$group": {
"_id": {
"state": "$state"
},
"this_path": {
"$first": {
"$concat": [
"events.",
"$state",
".0.date"
]
}
}
}
})
"this_path" gets "events.1.0.date", but how to use this value, in another query(line), I would like to do like a string interpolation. Some thing like
...
"date": {
"$first": { `\$${this_path}`}
...
so it become the "events.1.date" then "$events.1.0.date" then "123.2"
you can define it by let just for example a fragment from pipeline:
$lookup: {
from: contentCollectionName,
as: 'content',
let: {
parentId: '$id',
},
The id is taken from above matched documents, but it can be anything
This is my json:
{
"senderName": "ifelse",
"message": "Hi",
"groups": [
{
"id": 14,
"groupname": "Angular",
"contactgroups": [
{
"id": 1,
"contact": {
"id": 1,
"gsm": "123456789"
}
},
{
"id": 3,
"contact": {
"id": 2,
"gsm": "111111111"
}
}],
"select": true
}],
"draftData": {
"contacts": [
]
}
}
How to make the above json into:
[{phoneno: 123456789; sender: ifelse ; message: Hi},{phoneno: 11111111; sender: ifelse ; message: Hi}]
I want to take phoneno data from gsm object key
Which is best method to do this? for or forEach or anyother?
I guess, this is what you want. Use map to convert contactgroups to new array with phoneno.
var data = {
"senderName": "ifelse",
"message": "Hi",
"groups": [{
"id": 14,
"groupname": "Angular",
"contactgroups": [{
"id": 1,
"contact": {
"id": 1,
"gsm": "123456789"
}
},
{
"id": 3,
"contact": {
"id": 2,
"gsm": "111111111"
}
}
],
"select": true
}],
"draftData": {
"contacts": []
}
}
var result = data.groups[0].contactgroups.map(i => {
return {
phoneno: i.contact.gsm,
sender: data.senderName,
message: data.message
}
})
console.log(result);
I'm trying to figure out how to merge two JSON objects and create a merged object based on conditionals.
I want to loop through leadlistArray, get the rules.id for each entry and match it against rulesArray. The final outcome should be the finalArray.
leadlistArray = {
"status": "success",
"data": {
"leadlists": [
{
"rules": [
{
"$oid": "53866d4d1fd21c3cc41f52da"
}
],
"name": "List 1: only general rule"
},
{
"rules": [
{
"$oid": "53866d4d1fd21c3cc41f52da"
},
{
"$oid": "53866d9c1fd21c3cc79bf7ce"
},
{
"$oid": "53866d9c1fd21c3cc79bf2cd"
}
],
"name": "List 2: general and web-based rule"
},
{
"rules": [
{
"$oid": "53866d9c1fd21c3cc79bf7ce"
}
],
"name": "List 3: only web-based rule"
}
]
}
}
Rules array:
rulesArray = {
"status": "success",
"data": {
"rules": [
{
"description": "optimizely no!",
"start_time": "",
"values": [
"optimizely"
],
"end_time": "",
"operator": "",
"_id": {
"$oid": "53866d4d1fd21c3cc41f52da"
},
"type": 0,
"condition": 1
},
{
"description": "google_plus no!",
"start_time": "",
"values": [
"google_plus"
],
"end_time": "",
"operator": "",
"_id": {
"$oid": "53866d9c1fd21c3cc79bf2cd"
},
"type": 1,
"condition": 1
},
{
"description": "google_plus yes!",
"start_time": "",
"values": [
"google_plus"
],
"end_time": "",
"operator": "",
"_id": {
"$oid": "53866d9c1fd21c3cc79bf7ce"
},
"type": 1,
"condition": 0
}
]
}
}
the finalArray should, for each list, group the rules descriptions by rule type:
finalArray = [
{'name': 'List 1: only general rule'
,'rules':
{0: [
{'description': 'optimizely no!'}
]}
},
{'name': 'List 2: general and web-based rule'
,'rules':
{0: [
{'description': 'optimizely no!'}
], 1: [
{'description': 'google_plus yes!'},
{'description': 'google_plus no!'}
]}
},
{'name': 'List 3: only web-based rule'
,'rules': {
1: [
{'description': 'google_plus yes!'}
]}
}
]
Try this http://jsfiddle.net/eN52v/
var rules = rulesArray.data.rules
.reduce(function(acc, item){
return acc[item._id.$oid] = {
description: item.description,
type: item.type}, acc;
}, {}), //cache rules by id to avoid array lookups.
finalArray = leadlistArray.data.leadlists.map(function(item) {
return {
name: item.name,
rules: item.rules.reduce(function(acc, rule){ //Why not an array?
var ruleDesc = rules[rule.$oid],
type = ruleDesc.type;
(acc[type] || (acc[type] = [])).push({description: ruleDesc.description});
return acc;}, {})
}});