Use mongodb $in operator to search using array of arrays - arrays

var foo = [ [ 14, 31, 55, 56, 60, 19 ], [30, 32, 33, 50, 64, 6 ], [9, 15, 22, 35, 48, 3] ];
var bar = await Model.find({
numbers: { $in: foo }
});
console.log(bar);
When I try to run the above code above I get the error below. The model is a mongoose model and the query runs with no problems in a raw mongodb query using robomongo.
{ [CastError: Cast to number failed for value "14,31,55,56,60,19" at path "numbers"]
message: 'Cast to number failed for value "14,31,55,56,60,19" at path "numbers"',
name: 'CastError',
kind: 'number',
value: [ 14, 31, 55, 56, 60, 19 ],
path: 'numbers',
reason: undefined }

You're passing foo, which is an array of arrays to Mongoose with a property which is expecting a Number value, thus Mongoose is complaining to you when it does it's validation by saying that an array is not a number.

Related

MongoDB query all documents contains ids that does not exist anymore in the collection

I ran into an issue that I haven't found a solution to yet.
I have a collection with dozens of documents that every one of the documents contains a list (let's use the name 'list' as a key for that list) with ids of other documents(they are connected in some way).
some of the documents in the collection were deleted and I try to find all the documents that contain the ids of documents that do not exist anymore in the collection.
example:
As to the example above: I want to get the document with the id : 5e3266e9bd724a000107a902 because it contains a list with the id 5e32a7f7bd724a00012c1104 that does not exist anymore.
Here is a solution that works exploiting $lookup on the same collection (think "self-JOIN"):
var r = [
{_id: 0, aa: [ 10, 11, 12 ] }
,{_id: 1, aa: [ 10, 11, 12 ] }
,{_id: 2, aa: [ 20, 21, 22 ] } // 21 is on watch list...
,{_id: 3, aa: [ 21, 20, 12 ] } // this one too and 21 is in different position
,{_id: 4, aa: [ 10, 22, 12 ] }
,{_id: 5, aa: [ 10, 22, 23 ] } // this one too...
,{_id: 6, aa: [ 10, 22, 21, 23 ] } // this one has BOTH 21 and 23
,{_id: 10, X:10}
,{_id: 11, X:11}
,{_id: 12, X:12}
,{_id: 20, X:20}
,{_id: 21, X:21}
,{_id: 22, X:22}
,{_id: 23, X:23}
];
db.foo.insert(r);
// Here is the whole thing:
db.foo.aggregate([ ]);
// Delete _id 21 and 23:
db.foo.remove({_id: 21});
db.foo.remove({_id: 23});
// Double check:
c = db.foo.aggregate([ ]);
// Where does id 21 and/or 23 NOT exist anymore? Note we don't ask for 21 or 23.
// We just know we expect a query to return docs that indicate 21 and/or 23
// are no longer there:
c = db.foo.aggregate([
// NOTE! By using localField:'aa', we are asking for EACH element in the
// array to be used as a value to match to _id (in the same collection):
{$lookup: {from: 'foo', localField: 'aa', foreignField: '_id', as: 'X'}},
// Exploit "make a list of scalars from array of objects" notation by taking
// input array $X and taking the _id field out:
{$project: {X: {$setDifference: ["$aa", "$X._id"] }} },
// Keep those that match -- and protect against against empty sets
// with $ifNull to turn a null into an array of len 0:
{$match: {$expr: {$gt:[{$size: {$ifNull:['$X',[]]}}, 0]}} }
]);
{ "_id" : 2, "X" : [ 21 ] }
{ "_id" : 3, "X" : [ 21 ] }
{ "_id" : 5, "X" : [ 23 ] }
{ "_id" : 6, "X" : [ 21, 23 ] }

Searching two arrays and pushing values to a new one

I currently have two arrays formatted as below:
FreeChampions [
15,
17,
21,
27,
37,
45,
61,
72,
78,
89,
92,
99,
102,
121,
142
]
and
ChampionList [
[
"266",
"Aatrox",
"Aatrox"
],
[
"103",
"Ahri",
"Ahri"
],
[
"84",
"Akali",
"Akali"
],
[
"12",
"Alistar",
"Alistar"
],
[
"32",
"Amumu",
"Amumu"
],
[
"34",
"Anivia",
"Anivia"
],
[
"1",
"Annie",
"Annie"
],
I want to use the values of the FreeChampions Array to extract the object with the same value in the ChampionList array.
and then store those values in a new array.
This line should give you what you need. The filter filters out any elements who do not satisfy the find condition of the element being included in the FreeChampions array. Due to the type mismatch the y value needs to be cast to int for the includes to return truthy. Note that this will only work if the positions of the fields are static, ie the number in the inner array of ChampionsList is always at position 0.
var foundItems = ChampionList.filter(x => x.find(y => FreeChampions.includes(parseInt(y, 10))));

Flutter/Dart. Getting data between two dates in mongodb without using ISODATE

I'm using mongo 3.6. I have a DB with a field named date of type Date, and I want to return all my documents ($find) between two specific dates.
The problem is that I have to compose these queries in Flutter/Dart, so I cannot use ISODATE() to parse my dates because (to my knowledge) there's no such function, although I do have tried .toIso8601String with no luck.
Everything I try leads to and empty response (no documents returned, but no error also).
Things that I've tried:
{"date": {"$gte": "2018-08-23T09:34:32.000Z"}}
{"date": {"$gte": [{ "$dateFromString": { "dateString": "$date" }}, "2018-08-23T09:34:32.000Z"]}}
{"date": {"$gte": {"$date":"2018-08-23T09:34:32.000Z"}}}
{"date": {"$gte": {"$date":"2018-08-23 09:34:32.000"}}}
And many more.
Please, does anyone know how to solve this?
Thanks in advance
You might using mongo_dart package and see its repository (specially the method testDateTime). Then you could do something as:
var collectionName = 'users';
var collection = db.collection(collectionName);
await collection.insertAll([
{ 'name': 'John', 'birthday_date': DateTime.utc(2018, 8, 23, 12, 00, 00) },
{ 'name': 'Alice', 'birthday_date': DateTime.utc(2018, 8, 23, 10, 43, 24) },
{ 'name': 'Jim', 'birthday_date': DateTime.utc(2018, 8, 23, 8, 30, 0) }
]);
var result = await collection
.find(where.gte('birthday_date', DateTime.utc(2018, 8, 23, 9, 34, 32)))
.toList();

JS - group, map and flatten array of objects with arrays in them

Dears,
I really am not able to achieve following result:
{"2002":[1,2,3...]},
{"2003":[1,2,3,4...]},
...
I have following data (short example below):
{ "Year": "28-01-2020", "numbers": [10, 12, 20, 32, 35, 37] },
{ "Year": "03-10-2019", "numbers": [1, 6, 16, 19, 20, 30] },
{ "Year": "11-01-2018", "numbers": [14, 21, 25, 27, 30, 39] },
{ "Year": "11-08-2015", "numbers": [8, 16, 17, 18, 38, 46] },
I managed to use lodash _.groupBy to achieve following mid result:
[{…}]
0:
2000: Array(4)
0:
Year: "2000"
numbers: (7) [empty, 6, 25, 27, 37, 48, 49]
Year: "2000"
numbers: (7) [empty, 7, 12, 19, 30, 45, 49]
2: {Year: "2000", numbers: Array(7)}
2001: Array(104)
[0 … 99]
[100 … 103]
100: {Year: "2001", numbers: Array(7)}
101: {Year: "2001", numbers: Array(7)}
[0 … 99]
0: {Year: "2002", numbers: Array(7)}
1: {Year: "2002", numbers: Array(7)}
..
but i would like to have one object per year, with all numbers that appeared in sub arrays in this year
Could you please help me, i have tired ES6 map and for in loops, but non give me the proper result
Thank you in advance
This worked for me:
const slicedYear = results.map(elem => (elem = { Year: elem.Year.slice(elem.Year.length - 4), numbers: elem.numbers }));
const groupedResults = _.groupBy(slicedYear, 'Year');
for (let key in groupedResults) {
const selectedNumbers = { [key]: [groupedResults[key].map(elem => elem.numbers)].flat(2) };
}

Sum an array of hashes in Ruby

I have a ruby array of 3 hashes. Each peace has information about report_data (consumption of 2 types of energy) and monthes_data (same for each). Please see the code below.
arr = [{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[10, 20, 30, 40]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[20, 30, 40, 50]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[15, 25, 35, 45]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[25, 35, 45, 55]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[17, 27, 37, 47]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[27, 37, 47, 57]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}]
I'm new to Ruby. Please help me to sum all the data by energy types. In the end I want to have one hash with report_data and monthes_data. I need the result look like:
{:report_data=>
[{:type=>
{:"id"=>1, "name"=>"electricity"},
:data=>[42, 72, 102, 132]},
{:type=>
{"id"=>2, "name"=>"water"}},
:data=>[72, 102, 132, 162]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}}
arr = [{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[10, 20, 30, 40]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[20, 30, 40, 50]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[15, 25, 35, 45]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[25, 35, 45, 55]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[17, 27, 37, 47]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[27, 37, 47, 57]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}}]
acc = {}
arr.each do
|e| e[:report_data].each_with_index do
|e, idx|
type = e[:type]['id']
e[:data].each_with_index do
|e, idx|
acc[type] = [] if not acc[type]
acc[type][idx] = (acc[type][idx] or 0) + e
end
end
end
p acc
outputs
{1=>[42, 72, 102, 132], 2=>[72, 102, 132, 162]}
You should be able to reformat this into your record
Code
def convert(arr)
{ :months_data=>arr.first[:months_data],
:report_data=>arr.map { |h| h[:report_data] }.
transpose.
map { |d| { :type=>d.first[:type] }.
merge(:data=>d.map { |g| g[:data] }.transpose.map { |a| a.reduce(:+) }) }
}
end
Example
Half the battle in problems such as this one is visualizing the data. It's much clearer, imo, when written like this:
arr = [
{:report_data=>[
{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[10, 20, 30, 40]},
{:type=>{"id"=>2, "name"=>"water"}, :data=>[20, 30, 40, 50]}
],
:months_data=>{:months=>["jan", "feb"]}
},
{:report_data=>[
{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[15, 25, 35, 45]},
{:type=>{"id"=>2, "name"=>"water"}, :data=>[25, 35, 45, 55]}
],
:months_data=>{:months=>["jan", "feb"]}
},
{:report_data=>[
{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[17, 27, 37, 47]},
{:type=>{"id"=>2, "name"=>"water"}, :data=>[27, 37, 47, 57]}],
:months_data=>{:months=>["jan", "feb"]}
}
]
Let's try it:
convert(arr)
#=> {:months_data=>{:months=>["jan", "feb"]},
# :report_data=>[
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[42, 72, 102, 132]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[72, 102, 132, 162]}
# ]
# }
Explanation
The first thing I did is concentrate on computing the sums, so I converted this to the values of :report_data. That key, and the key-value pair of months' data, which is the same for all elements (hashes) of arr, can be added back in later.
b = arr.map { |h| h[:report_data] }
#=> [
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[10, 20, 30, 40]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[20, 30, 40, 50]}
# ],
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[15, 25, 35, 45]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[25, 35, 45, 55]}
# ],
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[17, 27, 37, 47]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[27, 37, 47, 57]}
# ]
# ]
If you are not certain that the elements of each array will be sorted by "id", you could write:
b = arr.map { |h| h[:report_data].sort_by { |g| g[:type]["id"] } }
c = b.transpose
#=> [
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[10, 20, 30, 40]},
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[15, 25, 35, 45]},
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[17, 27, 37, 47]}
# ],
# [{:type=>{"id"=>2, "name"=>"water"}, :data=>[20, 30, 40, 50]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[25, 35, 45, 55]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[27, 37, 47, 57]}
# ]
# ]
e = c.map {|d| { :type=>d.first[:type] }.
merge(:data=>d.map { |g| g[:data] }.transpose.map { |a| a.reduce(:+) }) }
#=> [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[42, 72, 102, 132]},
# {:type=>{"id"=>2, "name"=>"water"} , :data=>[72, 102, 132, 162]}]
Lastly, we need to put the put the key :report_data back in and add the months' data:
{ :months_data=>arr.first[:months_data], :report_data=>e }
#=> {:months_data=>{:months=>["jan", "feb"]},
# :report_data=>[
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[42, 72, 102, 132]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[72, 102, 132, 162]}
# ]
# }
For clarity I've reformatted the input array and removed the :monthes_data key, since that seems to be unrelated to your question. Here's our data:
TL;DR
def zip_sum(arr1, arr2)
return arr2 if arr1.nil?
arr1.zip(arr2).map {|a, b| a + b }
end
def sum_report_data(arr)
arr.flat_map do |item|
item[:report_data].map {|datum| datum.values_at(:type, :data) }
end
.reduce({}) do |sums, (type, data)|
sums.merge(type => data) do |_, old_data, new_data|
zip_sum(old_data, new_data)
end
end
.map {|type, data| { type: type, data: data } }
end
p sum_report_data(arr)
# =>
[ { type: { "id" => 1, "name" => "electricity" }, data: [ 42, 72, 102, 132 ] },
{ type: { "id" => 2, "name" => "water" }, data: [ 72, 102, 132, 162 ] }
]
Explanation
arr = [
{ report_data: [
{ type: { "id" => 1, "name" => "electricity" },
data: [ 10, 20, 30, 40 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 20, 30, 40, 50 ]
}
]
},
{ report_data: [
{ type: { "id" => 1, "name" => "electricity" },
data: [ 15, 25, 35, 45 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 25, 35, 45, 55 ]
}
]
},
{ report_data: [
{ type: { "id" => 1, "name" => "electricity" },
data: [ 17, 27, 37, 47 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 27, 37, 47, 57 ]
}
]
}
]
Step 1
First, let's define a helper method to sum the values of two arrays:
def zip_sum(arr1, arr2)
return arr2 if arr1.nil?
arr1.zip(arr2).map {|a, b| a + b }
end
zip_sum([ 1, 2, 3 ], [ 10, 20, 30 ])
# => [ 11, 22, 33 ]
zip_sum(nil, [ 5, 6, 7 ])
# => [ 5, 6, 7 ]
The way zip_sum works is by "zipping" the two arrays together using Enumerable#zip (e.g. [1, 2].zip([10, 20]) returns [ [1, 10], [2, 20] ]), then adding each pair together.
Step 2
Next, let's use Enumerable#flat_map to get the parts of the data we care about:
result1 = arr.flat_map do |item|
item[:report_data].map {|datum| datum.values_at(:type, :data) }
end
# result1 =>
[ [ { "id" => 1, "name" => "electricity" }, [ 10, 20, 30, 40 ] ],
[ { "id" => 2, "name" => "water" }, [ 20, 30, 40, 50 ] ],
[ { "id" => 1, "name" => "electricity" }, [ 15, 25, 35, 45 ] ],
[ { "id" => 2, "name" => "water" }, [ 25, 35, 45, 55 ] ],
[ { "id" => 1, "name" => "electricity" }, [ 17, 27, 37, 47 ] ],
[ { "id" => 2, "name" => "water" }, [ 27, 37, 47, 57 ] ]
]
Above we've just grabbed the :type and :data values out of each hash the :report_data arrays.
Step 3
Next let's use Enumerable#reduce to iterate over the array of arrays and calculate a running sum of the :data values using the zip_sum method we defined earlier:
result2 = result1.reduce({}) do |sums, (type, data)|
sums.merge(type => data) do |_, old_data, new_data|
zip_sum(old_data, new_data)
end
end
# result2 =>
{ { "id" => 1, "name" => "electricity" } => [ 42, 72, 102, 132 ],
{ "id" => 2, "name" => "water" } => [ 72, 102, 132, 162 ]
}
The result might look a little odd to you because we usually use strings or symbols as hash keys, but in this hash we're using other hashes (the :type values from above) as keys. That's one nice thing about Ruby: You can use any object as a key in a hash.
Inside the reduce block, sums is the hash that's ultimately returned. It starts out as an empty hash ({}, the value we passed to reduce as an argument). type is the hash we're using as a key and data is the array of integers. In each iteration the next values from the result2 array are assigned to type, but sums is updated with whatever value was returned at the end of the block in the previous iteration.
We're using Hash#merge in kind of a tricky way:
sums.merge(type => data) do |_, old_data, new_data|
zip_sum(old_data, new_data)
end
This merges the hash { type => data } (remember that type is the :type hash
and data is the array of integers) into the hash sums. If there are any key collisions, the block will be invoked. Since we only have one key, type, then the block will be invoked if sums[type] already exists. If it does, we call zip_sum with the previous value of sums[type] and data, effectively keeping a running sum of data.
In effect, it's basically doing this:
sums = {}
type, data = result2[0]
sums[type] = zip_sum(sums[type], data)
type, data = result2[1]
sums[type] = zip_sum(sums[type], data)
type, data = result2[3]
# ...and so on.
Step 4
We now have this hash in result3:
{ { "id" => 1, "name" => "electricity" } => [ 42, 72, 102, 132 ],
{ "id" => 2, "name" => "water" } => [ 72, 102, 132, 162 ]
}
That's the data we want, so now we just have to take it out of this weird format and put it into a regular hash with the keys :type and :data:
result3 = result2.map {|type, data| { type: type, data: data } }
# result3 =>
[ { type: { "id" => 1, "name" => "electricity" },
data: [ 42, 72, 102, 132 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 72, 102, 132, 162 ]
}
]

Resources