This question already has answers here:
Remove array element based on object property
(12 answers)
Closed 2 years ago.
I have a JSON as below.
{
"key" : "Balance",
"translation" : [
{
"language" : "English",
"value" : "abc"
},
{
"language" : "German",
"value" : "faq"
}
]
}
I'm passing the language "English" and the language and its value should delete. There are many keys so I'm using for loop in my function to get that language.
this._work.forEach( ( translation ) => {
translation.translation.forEach( data => delete data.language[ language ] );
} );
this._work contains the above json value. Here the language and its value not deleting. What I'm missing?
Aside note: There is nothing called JSON en js, what you have is a js object.
Actually you're trying to delete the attribute "English" from the string language, which is wrong and doesn't make sense.
For example:
delete data.language["English"]
I recommend you to use the function Array.prototype.filter as follow:
let data = {
"key": "Balance",
"translation": [{
"language": "English",
"value": "abc"
},
{
"language": "German",
"value": "faq"
}
]
},
language = "English";
data.translation = data.translation.filter(({language: lang}) => lang !== language);
console.log(data);
In your solution, delete data.language[ language ] will not work since data.language['English'] is undefined. This deletion technique is generally used when you want to remove by using key from an object, not row in an array.
You can use filter instead of using forEach.
this._work.forEach( ( row ) => {
row.translation = row.translation.filter( translation => translation.language !== translation );
} );
{
property: 'value',
language: 'English'
}
delete instruction needs a property provided to be deleted. like delete data.property or delete data.language. In the code, you provided data.language[language] points to a value. If you want to delete the language property if the value is English, then the code you needs is
this._work.forEach( ( translation ) => {
translation.translation.forEach( data => if (language === data.language) { delete data.language; });
} );
as a result you would get:
{
"key" : "Balance",
"translation" : [
{
"value" : "abc"
},
{
"language" : "German",
"value" : "faq"
}
]
}
Related
Hi first of all I would like to state that yes, this question has been answered here Update value of a nested dictionary of varying depth however, this is in Python.
I would like to know if there's a Javascript approach to solving this issue.
Let's say I have a dictionary as such:
{
"group1" : {
"name" : "NAME1"
},
"group2" : {
"name" : "NAME2"
}
}
How would I be able to replace "NAME1" with lets say "Bob White" without doing this dict["group1"]["name"] = "Bob White"?
Could someone help please? Thanks
You can loop through each property and set the value of its name property:
const obj = {
"group1" : {
"name" : "NAME1"
},
"group2" : {
"name" : "NAME2"
}
}
Object.keys(obj).forEach(e => obj[e].name = "Bob White")
console.log(obj)
{
"_id":{"$oid":"5f5287db8c4dbe22383eca58"},
"__v":0,
"createdAt":{"$date":"2020-09-12T11:35:45.965Z"},
"data":["Buy RAM","Money buys freedom"],
"updatedAt":{"$date":"2020-09-12T11:38:10.637Z"}
}
I want to update the first element in this data array field as Buy SSD.
How can I do it using NodeJS?
db.collection.findOneAndUpdate({
"_id.$oid": "5f5287db8c4dbe22383eca58",
data: "Buy RAM"
}, {
$set: {
"data.$" "Buy SSD"
}
})
This query updates the first element in the data array inside the document that matches "_id.$oid": "5f5287db8c4dbe22383eca58", using $ positional identifier and sets it to the new value.
For more Array Update Operators in mongodb, here is a reference: mongodb manual
You can use filtered positional operator
db.collectionName.updateOne(
{
"_id.$oid": "5f5287db8c4dbe22383eca58"
},
{
$set: {
"data.$[element]": "Buy SSD"
}
},
{
{ arrayFilters: [ { element: "Buy Ram" } ] }
})
Caution: It will update all array element matching the text. In this case, "Buy Ram"
You can use str.replace()
var product =
[{
"_id":{"$oid":"5f5287db8c4dbe22383eca58"},
"__v":"0",
"createdAt":{"$date":"2020-09-12T11:35:45.965Z"},
"data":["Buy RAM","Money buys freedom"],
"updatedAt":{"$date":"2020-09-12T11:38:10.637Z"}
}]
var new_product = JSON.stringify(product).replace("Buy RAM", "Something");
console.log(new_product);
UpdateOne -> Updates a single Document:
db.collection.updateOne(filter, update, options)
You will probably gonna filter using the _id field, and use $set to update the specific field.
Use the dot-notation to access and set fields deep inside objects, without affecting the other properties of those objects.
you want to update the 1st array entry in "data", and array keys are 0 indexed - that's the key 0.
so the query will look something like that:
db.collection.update(
{ _id: { "$oid": "56476e04e5f19d86ece5b81d"}, // probb ObjectId Instance
{ $set:
{
"data.0": "Buy SSD" // Using dot-notation
}
}
)
for more advanced use, you can use the MongoDB's positional operator $ without explicitly specifying the position of the element in the array.
The positional operator allows you to use a condition like this:
{"Order.name": "test"}
and then reference the found array entry like so:
{"Order.$ // <- the dollar represents the first matching array key index
Example:
/* DATA
{
"_id" : "43434",
"Order" : [
{"name" : "test", "items" : ["", "new_value", "" ]},
{"name" : "test2", "items" : ["", "", "" ]}
]
}
*/
db.collection.update(
{ _id: "43434", "Order.name": "test2"},
{ $set:
{
"Order.$.items.1": "new_value2" // positional operator & dot-notation.
}
}
)
>>> db.collection.find()
{
"_id" : "43434",
"Order" : [
{"name" : "test", "items" : ["", "new_value", "" ]},
{"name" : "test2", "items" : ["", "new_value2", "" ]}
]
}
I am new to queries in mongodb. I have a document like this -
{
"_id" : ObjectId("5eb0f70f88cd051e7839325c"),
"id" : "1",
"arrayInfo" : [ {"color":"red"}, {"color":"black"}, {"color":"cyan"} ]
}
There are many documents in this format with changing ids and colors inside arrayInfo. I want to do something like -
Find record with id "1" -> Display object inside array info with {"color" : "cyan"}
I believe I have to chain queries after finding like this -
db.collection('Records').findOne({id:"1"}).**something**
Any help will be appreciated thanks.
if(id===1){
res.arrayInfo.map(item => console.log(item.color))
}
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
enter link description here
If no operator is specified, MongoDB by default performs array element matching when the document stores an array. Thus you can simply do:
MongoDB Enterprise ruby-driver-rs:PRIMARY> db.foo.findOne({id:'1',arrayInfo:{color:'cyan'}})
{
"_id" : ObjectId("5eb0f70f88cd051e7839325c"),
"id" : "1",
"arrayInfo" : [
{
"color" : "red"
},
{
"color" : "black"
},
{
"color" : "cyan"
}
]
}
To match one field in the array instead of the complete array element, use $elemMatch.
I want to fetch all the names and label from JSON without loop. Is there a way to fetch with any filter method?
"sections": [
{
"id": "62ee1779",
"name": "Drinks",
"items": [
{
"id": "1902b625",
"name": "Cold Brew",
"optionSets": [
{
"id": "45f2a845-c83b-49c2-90ae-a227dfb7c513",
"label": "Choose a size",
},
{
"id": "af171c34-4ca8-4374-82bf-a418396e375c",
"label": "Additional Toppings",
},
],
},
]
}
When you say "without loops" I take it as without For Loops. because any kind of traversal of arrays, let alone nested traversal, involve iterating.
You can use the reduce method to have it done for you internally and give you the format you need.
Try this :
const data = {
sections: [
{
id: "62ee1779",
name: "Drinks",
items: [
{
id: "1902b625",
name: "Cold Brew",
optionSets: [
{
id: "45f2a845-c83b-49c2-90ae-a227dfb7c513",
label: "Choose a size"
},
{
id: "af171c34-4ca8-4374-82bf-a418396e375c",
label: "Additional Toppings"
}
]
}
]
}
]
};
x = data.sections.reduce((acc, ele) => {
acc.push(ele.name);
otherName = ele.items.reduce((acc2, elem2) => {
acc2.push(elem2.name);
label = elem2.optionSets.reduce((acc3, elem3) => {
acc3.push(elem3.label);
return acc3;
}, []);
return acc2.concat(label);
}, []);
return acc.concat(otherName);
}, []);
console.log(x);
Go ahead and press run snippet to see if this matches your desired output.
For More on info reduce method
In the context of cJSON
yes, we can fetch the key value for any of the object.
1 - each key value is pointed by one of the objects. will simply fetch that object and from there will get the key value.
In the above case for
pre-requisition: root must contain the json format and root must be the cJSON pointer. if not we can define it and use cJSON_Parse() to parse the json.
1st name object is "sections" will use
cJSON *test = cJSON_GetObjectItem(root, "sections");
char *name1 = cJSON_GetObjectItem(test, "name" )->valuestring;
2nd name key value
cJSON *test2 = cJSON_GetObjectItem(test, "items");
char *name2 = cJSON_GetObjectItem(tes2, "name")->valuestring;
likewise, we can do for others as well to fetch the key value.
I have this data:
{
"_id" : ObjectId("5461e16ee7caf96f8f3584a2"),
"num_marcacao" : "100",
"sexo" : "Fêmea",
"idade" : "20",
"bigdata" : {
"abortos" : [
{
"data_aborto" : "2014-11-11",
"causa_aborto" : "Aborto causa 1"
},
{
"data_aborto" : "2014-09-01",
"causa_aborto" : "Aborto causa 2"
}
],
"crias" : [
ObjectId("5461e16ee7caf96f8f3584a2")
]
}
}
{
"_id" : ObjectId("5461e1cae7caf96f8f3584a4"),
"num_marcacao" : "200",
"sexo" : "Fêmea",
"bigdata" : {
"crias" : [
ObjectId("5461e1f3e7caf96f8f3584a5"),
ObjectId("5461e760e7caf96f8f3584a6")
]
}
}
Using the following distinct function I get one result
db.animal.distinct('_id', {'bigdata.crias':{$exists:true}}
Result:
{
"0" : ObjectId("5461e16ee7caf96f8f3584a2"),
"1" : ObjectId("5461e1cae7caf96f8f3584a4")
}
Now I want to get the array that is in bigdata.crias like the result of the distinct query.
I'm trying to do like this:
db.animal.find(
{
$and: [
{'num_marcacao': '200'},
{'bigdata.crias':{$exists: true}}
]
},
{
'bigdata.crias': true,
'_id': false
}
)
But the result is not like the one I need. This is what it's returning:
{
"bigdata" : {
"crias" : [
ObjectId("5461e1f3e7caf96f8f3584a5"),
ObjectId("5461e760e7caf96f8f3584a6")
]
}
}
And I need
{
"0" : ObjectId("5461e1f3e7caf96f8f3584a5"),
"1" : ObjectId("5461e760e7caf96f8f3584a6")
}
Anyhow. MongoDB does not generally do this from either the .find() or .aggregate() methods or anything general around them. Only the .distinct() method invokes a special form where the result given is "truly" just an array of the specified "key" to be distinct on.
You can always "inspect" the object returned and just use the array element in the structure. You can also specify a "query" argument to the .distinct() command method in the first place:
db.collection.distinct(
"bigdata.crias",
{
"bigdata.crias": { "$exists": true },
"num_marcacao": "200"
}
);
Where you also see your $and argument is redundant. All MongoDB query arguments are an "and" implementation by default. You don't need this unless you are specifying "more than one" condition on the same "field name". That would result in an invalid object by breaking the basic "hash/map" "unique key" rule, and which is why and "array" is used for this form to keep it valid.