I want to extract the data based on keys,
Ex: I want order values, How to get it in node js
const json = {"order": {
"PRD_SHIRT_048": [
"price",
"productId",
"quantity"
],
"PRD_TOP_047": [
"price",
"productId",
"quantity"
]
}};
I'm not sure what exactly is your question, but if you want to access nested properties, you can do it like this:
json.order.PRD_SHIRT_048
This will return:
[
"price",
"productId",
"quantity"
]
And if you want to get all the keys of order object, you can use Object.keys(json.order)
This will return
["PRD_SHIRT_048", "PRD_TOP_047"]
Related
I'm trying to fetch documents from cloudant db with node js. Here for one single input value I'm able to get results, but with array of input's I'm confusing to write query.
This is my node js query part for single input:
var query = {
"selector":
{
"name": 'name1',
},
"fields": [
"_id",
"_rev",
"subjects",
"name"
],
};
For array of names:
var query = {
"selector":
{
"name": {$in: ['name1', 'name2']},
}
};
Here for name field input, i want to pass multiple names in an array. So that what ever names match those documents should return and the above query for multiple names is not working. Any help or suggestion will be appreciated.
If you want to match documents with either name1 or name2, then use the $or combination operator e.g.
var query = {
"selector":
{
"$or": [
{ "name": "name1" },
{ "name": "name2" }
]
}
};
I dont think there is $in operator but there is $all which will match array of values
Try
'$all': ['name1', 'name2']
I am really lost here. Please help me understand how I can extract specific objects (without a key) from nested json list.
Here is the structure of the json list:
"data":{
"result":{
"person":
[
[
{
"personName":
"jobs":
[
{
"employerName":
},
{
"employerName":
}
]
},
{
"siteName":
...
}
]
]
}
}
I have two array variables declared like so in my state container
PersonObject: Array<Person>,
JobsObject : Array<Jobs>
In my function, if I do this :
const res = //calls api
store.setState({PersonObject: res.body.data.result.person})
This will store the whole object in PersonObject
But I want to assign "person" object and its values to PersonObject and "jobs" to JobsObject. But there is no key assigned to the person.
When I try to look up the key's value by doing so
Object.getOwnPropertyNames(res);
//I get this as output
0: "0"
1: "length"
{
"_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 need to alter my JSON API from https://demo.piwik.org/?module=API&method=VisitsSummary.getVisits&idSite=7&period=day&date=last3&format=json&token_auth=anonymous
{"2017-12-21":767,"2017-12-22":571,"2017-12-23":31}
to a valid array for my charts in NVD3.js as
[ { "key" : "Page Visits" , "values" : [ [ 1025409600000 , 767] , [ 1028088000000 , 571] , [ 1030766400000 , 31] }]
NOTE: These dates do not match the JSON and Array but highlight the conversion needed, if anyone can explain to me the date format used in NVD3.js that would be great too.
If helpful, I can add the scripts used to get data and display the NVD3 Chart.
You can use array#map to convert your object to array. Then use [ { "key" : "Page Visits" , "values" : result }]; to get the required object.
var data = {"2017-12-21":767,"2017-12-22":571,"2017-12-23":31};
var result = Object.keys(data).map(k => [new Date(k).getTime(), data[k]]);
console.log(result);
I have a (nested) data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?
{
"data": [{
"name": "name1",
"value": "value1",
"list": [{
"sname": "sname1",
"svalue": "svalue1"
}, {
"sname": "sname2",
"svalue": "svalue2"
}]
}]
}
jQuery
var pk = $("#pk").val();
console.log(pk);
url = "/register/search?id=" + pk;
console.log(url);
$('#largeTable').DataTable({
"ajax": url,
"bDestroy": true,
"columns": [{
"data": "name"
},
{
"data": "value"
},
{
"data": "list.1.sname"
},
{
"data": "list.1.svalue"
},
{
"data": null,
"defaultContent": editview
}
]
});
Here it is possible to display either first or second list values by using list.1 or list.0
But I want two values at a time.
Also, how could I access the svalue of the second item in list?
I tried with data.list[1] but:
TypeError: data.list is undefined
Since data is an array, you should first get the item - and since you only have one item - you'd use data[0], and then get access to the list property like data[0].list[1] - this will give you the second item in the list - but since you are interested in a specific property (svalue) of this item, you will then access it like this: data[0].list[1].svalue.
A better approach would be to loop through the items in the data array - and then for each item, loop through the items in the list array. See #Rajesh's comment.
I hope that helps;
Specifically you can access it like this object.data[0].list[1].svalue. The reason data.list is undefined is because data corresponds to an array data: [{ }] this is why we use data[0], but data itself is a key in an object so before you can get to data you need to access it. If the objects name where data resides were object (like I did below) then it'd be accessed like this object.data[0]
const object = {
"data": [{
"name": "name1",
"value": "value1",
"list": [{
"sname": "sname1",
"svalue": "svalue1"
}, {
"sname": "sname2",
"svalue": "svalue2"
}]
}]
}
console.log(object.data[0].list[1].svalue)