I'm new to node.js and admittedly I'm probably trying to learn the hard way but I use the following code to parse json looking for the id when the corresponding name contains the uuid. The uuid gets passed from a different function.
var arrFound = Object.values(json).filter(function(obj) {
return obj.id && obj.name.includes(uuid);
}).map(function(obj) {
return obj.id;
});
console.log (arrFound); // array of matched id values
I expect this to return something like [ 'local-1517085058363' ].
Instead I get this:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
The json comes from the Spark HistoryServer API(http://127.0.0.1:18080/api/v1/applications), although it's just JSON so shouldn't matter where it comes from. Here is a sample:
[{
"id" : "local-1517085058363",
"name" : "TeraGen (5MB) 6e722700-0397-11e8-ae84-b54a3ebb27aa",
"attempts" : [ {
"startTime" : "2018-01-27T19:22:37.513GMT",
"endTime" : "2018-01-27T19:22:43.253GMT",
"lastUpdated" : "2018-01-27T19:22:43.000GMT",
"duration" : 5740,
"sparkUser" : "paulcarron",
"completed" : true,
"endTimeEpoch" : 1517080963253,
"startTimeEpoch" : 1517080957513,
"lastUpdatedEpoch" : 1517080963000
} ]
} ]
Also, I'm running my code on Gulp. I'm not sure if that will make any difference.
How do I fix this so I only return one single id?
There was multiple arrays so that explains where they came from. I used a reduce() to get the object I was looking for.
Related
In Mongo DB, how can I set values in an array of arrays when the index cannot be assumed?
For example, how can I set the IdleShutdown value to "2" (instead of "10") in this blob below?
What would the command look like to correctly find the item in the list and set the value without any assumptions that the current index will stay the same? I also cannot assume any values for the second item in the list (eg, "10" could become any value)
> db.EventPluginSettingsCollection.findOne({"_id": "spot"})
{
"_id" : "spot",
"LastWriteTime" : ISODate("2019-12-16T11:04:30.499Z"),
"Name" : "Spot",
"PluginEnabled" : 1,
"DebugLogging" : false,
"Icon" : null,
"Limits" : [ ],
"DlInit" : [
[
"State",
"Disabled"
],
[
"Config",
"{}"
],
[
"IdleShutdown",
"10"
],
[
"StaggerInstances",
"50"
],
[
"PreJobTaskMode",
"Conservative"
],
[
"AWSInstanceStatus",
"Disabled"
]
],
"Meta" : {
}
}
I understand I could replace the whole blob, and I could also use individual indexes for each item, but none of these approaches are future proof should the number of items in the list change in the future.
MongoDB 3.6 introduced the filtered positional operator array update operator, $[ < identifier> ], for these types of operations. Updates using this syntax will only update elements of an array that match a specified array filter.
For example:
> db.foo.update(
... { "_id" : "spot" },
... {
... "$set" : {
... // only updates array elements matching the filter below
... "DlInit.$[element]" : [ "IdleShutdown", "2"]
... }
... },
... {
... "arrayFilters": [
... // Search for DlInit array elements that are the specified array
... { "element.0" : "IdleShutdown" }
... ]
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find().pretty()
{
"_id" : "spot",
"LastWriteTime" : ISODate("2019-12-16T11:04:30.499Z"),
"Name" : "Spot",
"PluginEnabled" : 1,
"DebugLogging" : false,
"Icon" : null,
"Limits" : [ ],
"DlInit" : [
[
"State",
"Disabled"
],
[
"Config",
"{}"
],
[
"IdleShutdown",
"2"
],
[
"StaggerInstances",
"50"
],
[
"PreJobTaskMode",
"Conservative"
],
[
"AWSInstanceStatus",
"Disabled"
]
],
"Meta" : {
}
}
I have two arrays. In first all categories and in second there are selected categories. I want to match those objects and then :
- Either I want to merge selected array into main array
- Or I want new array mapped with selected data.
Here is my arrays :
self.arrAllCategories = [
[
"_id": “1”,
"category_name": "Table”,
"is_selected" : false
],
[
"_id": “2”,
"category_name": “Chair”,
"is_selected" : false
],
[
"_id": “3”,
"category_name": “Fan”,
"is_selected" : false
],
[
"_id": “4”,
"category_name": “WallFrame”,
"is_selected" : false
]
]
And so on….
And selected categories array :
self.arrSelectedCats = [
[
"_id": “1”,
"category_name": "Table”,
"is_selected" : true
],
[
"_id": “3”,
"category_name": “Fan”,
"is_selected" : true
],
]
I have to display categories screen with selected categories in different style and non selected categories in just gray look. So How I can merge and get one array to load data in collection view ?
let yourKey = "_id"
var nonSelected = arrAllCategories.filter{ !(arrSelectedCats.map { $0[yourKey] as? String }).contains(($0[yourKey] as? String)) }
print(nonSelected)
Try this :)
I need some help. I am trying to find an array in another array.
Records:
{
"_id" : ObjectId("56b7e6966213e8d142ef55b7"),
"users" : [
ObjectId("56b0e547a838b5a45b4d0100"), // 0
ObjectId("56a7e16e37e5adf32b97cc3d") // 1
]
},
{
"_id" : ObjectId("56b7e6966213e8d142ef55b7"),
"users" : [
ObjectId("56b0e547a838b5a45b4d0100"),
ObjectId("56a7e16e37e5adf32b97cc3d"),
ObjectId("56b7e6b96213e8d142ef55b8")
]
}
I'm trying to find only first record "_id" : ObjectId("56b7e6966213e8d142ef55b7"),
I'm using query:
db.collection.find({"users" : { $in: [
ObjectId("56b0e547a838b5a45b4d0100"),
ObjectId("56a7e16e37e5adf32b97cc3d")
]}})
or
db.collection.find({ $and: [{
"users": ObjectId("56b0e547a838b5a45b4d0100")
}, {
"users": ObjectId("56a7e16e37e5adf32b97cc3d")
}]})
Response gives me two records.
This request gives me the correct response:
db.collection.find({"users" : [
ObjectId("56b0e547a838b5a45b4d0100"), // 0
ObjectId("56a7e16e37e5adf32b97cc3d") // 1
]})
but if record has an array like this:
{
"_id" : ObjectId("56b7e6966213e8d142ef55b7"),
"users" : [
ObjectId("56a7e16e37e5adf32b97cc3d"), // 1
ObjectId("56b0e547a838b5a45b4d0100") // 0
]
}
it doesn't work
$all, $eq, $in also doesn't work
db.rooms.find({
users: {
$all: [
ObjectId("56a7e16e37e5adf32b97cc3d"),
ObjectId("56b0e547a838b5a45b4d0100")
]
}
})
I need to find rows where [1,2] == [1,2] or [1,2] == [2,1]
not where [1,2,3] == [1,2]
I will appreciate if somebody can help me.
If all you expect is the resulting document with "only" those two array entries, then the combination of conditions you want is $all and $size:
db.rooms.find({
"users": {
"$all": [
ObjectId("56b0e547a838b5a45b4d0100"),
ObjectId("56a7e16e37e5adf32b97cc3d")
],
"$size": 2
}
})
Where $all is basically an $and with shorter syntax, and the $size retricts that since the list as n entries, then you only want to match something of that particular length/size.
This is better than specifying the array directly, since it will match in either order, as opposed to this statement that would not match considering the order is different and therefore not an "exact" match:
db.rooms.find({
"users": [
ObjectId("56a7e16e37e5adf32b97cc3d"),
ObjectId("56b0e547a838b5a45b4d0100")
]
})
So logically the array contains "all" the specified entries, and is the same "size" as the input and no different.
I want to iterate over a nested array I created in Freemarker but can't figure out how to print each value.
The array var
<#assign filters = [
{ "p1" : [ "performance" ] },
{ "p2" : [ "nav" ] },
{ "p3" : [ "quarterly" ] },
{ "f1" : [ "50496", "50497", "50501" ]},
{ "f2" : [ "51202", "51196", "51203", "51246", "51207", "51260" ]},
{ "f3" : [ "50504", "50505", "50506" ]},
{ "f4" : [ "50507", "50508", "50509", "50510", "50512" ]}
]>
Iterating over the array
<#list filters as filter>
${filter}'<#sep>,
</#list>
I want to create an output that looks like this:
$.plugin.function({
p1: 'performance',
p2: 'nav',
p3: 'quarterly',
fi: '50496' + '|' + '50497' + '|' + '50501',
f2: etc....
});
My error is this:
For "${...}" content: Expected a string or something automatically
convertible to string (number, date or boolean),
but this has evaluated to an extended_hash
Any suggestions?
The issue is that Freemarker doesn't have a way to convert filter (a hash) into a string. I think that you want the keys function. Where you now have
${filter}'<#sep>,
You'd instead call
<#list filter?keys as prop>
...
</#list>
and iterate over those keys. In your example there would only be one key per filter and the value
filter[prop]
would be the array, that you'd also have to iterate over.
See Freemarker iterating over hashmap keys
I think you overcomplicate the data structure by using a list of hash-es. Instead, just use a hash:
<#assign filters = {
"p1" : [ "performance" ],
"p2" : [ "nav" ],
"p3" : [ "quarterly" ],
"f1" : [ "50496", "50497", "50501" ],
"f2" : [ "51202", "51196", "51203", "51246", "51207", "51260" ],
"f3" : [ "50504", "50505", "50506" ],
"f4" : [ "50507", "50508", "50509", "50510", "50512" ]
}>
<#list filters?keys as name>
${name}: <#list filters[name] as value>'${value}'<#sep> + '|' + </#list><#sep>,
</#list>
(Another possibility would be a list of lists, but that's less natural.)
How would I query the the followoing:
get the value of id '120154' for document '123' (result should be 32')
Collection Name:'Collection'
{
"_id" : "123",
"Data" : {
"_v" : [
[
120154,
32
],
[
120156,
"0.940515536000"
],
[
120157,
ISODate("2013-12-26T00:00:00Z")
],
[
120158,
"ABX-HE-AA 06-1"
],
[
120159,
"0A08AG9A4"
]
]}}
db.collection.find({"_id" : "123"},{"Data._v.$1" : {"$in" : {120154}}}). I think this should work when u know the _id for the document.
I had the same problem and so far I'm unable to find the solution because mongodb queries always return a whole document but you can project fields but that won't return you 32. So when you query {_id : 123} AND {no matter what else}, It will return you the matching whole documents not inner arrays.and I might be wrong but that's what I've experienced