Javascript - Update value in nested dictionary regardless of depth - javascript

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)

Related

How to update specific data in array in MongoDB?

{
"_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", "" ]}
]
}

Delete a json value which is inside a set of json [duplicate]

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"
}
]
}

Mongodb native driver chain queries

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.

How to target property in JavaScript object

I have the following problem. I'm sure the solution is rather simple, but I just can't figure it out.
So there is a object which goes like this:
var products = {
"productType1" : {
"productCode" : {
"name" : "Some Name 1",
"price" : "250"
},
"productCode2" : {
"name" : "Some Name 2",
"price" : "300"
},
"productCode3" : {
"name" : "Some Name 3",
"price" : "330"
}
}
}
And I try to match a "productCode" property in a "for in" loop with a variable. And then I just try to access the "name" or "price" property but in return I only get "undefined", although I do get match with a "productCode" property.
for(a in products.productType1){
if(finalCode === a){
console.log(a.name);
break;
}
else{
console.log("This is not the property you're looking for");
continue;
}
So the question is - how can I access the above mentioned properties in a loop?
a is a string containing the property name. It isn't the value of that property. You need to get the value first.
products.productType1[a].name

How to make MongoDB find nested json return as an array?

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.

Categories

Resources