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

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();

Related

karate framework - how to count number of arrays in json response

there
I have a JSON response as below:
"log": [{
"a": 0.40,
"b": "ED",
}, {
"c": 82,
"d": "ABC",
}, {
"e": 36,
"f": 23,
}, {
"g": 12,
"h": 40,
}
]
I need to count a number of lists in a 'log' object to be able to address the last one and find a specific element in it. The response is dynamic and has different amount of lists in it (in this case 4)
I tried log[-1], and examples form js-arrays.feature as in the link below:
https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature#L83
It is easy to find a number of elements in a list, but I frequently have variable amount of lists and I cant make it work
Many Thanks,
Take into account that log[-1] would return you undefined due to there is no index -1 in the array. For get the number of elements in your array you must do
log.length
Also about the link you've posted
log[log.length-1]; //This will return the last element of the array in this case { "g": 12, "h": 40, }

How to normalize an array of objects in the flattest way for the Redux?

Beware! The question may be confusing and not relevant because my
assumptions about the cause of the bug were wrong, the problem was in
reducer, not in a way I represented a data.
So, the correct answer for the question is jpdelatorre’s one, but
Joao’s is about the bug itself.
Let’s say I have JSON response from server which is an array of nested objects. How to flatten it out making store handling as easy as possible? I’ve tried to use a normalizr tool like this:
const imageSchema = new Schema('image', { idAttribute: 'id' });
const tooltipSchema = new Schema('tooltips', { idAttribute: 'id' });
imageSchema.define({
tooltips: arrayOf(tooltipSchema),
});
const initData = data.map(item => normalize(item, imageSchema));
But I believe I’m doing it wrong because it doesn’t help much. The store is still too complex and therefore I need to use some recursive stuff in reducer to update the state.
Moreover, a deeply nested state makes working with react-redux connect() very hard too, because it does only a shallow comparison.
The shape of the response is like:
[
{
"id": 1,
"title": "Partridge",
"tooltips": [
{
"id": 10,
"x": 0.56,
"y": 0.59,
"content": "Jacky"
},
{
"id": 11,
"x": 0.08,
"y": 0.8,
"content": "Sarah"
}
]
},
{
"id": 2,
"title": "The Great Seal of South Australia",
"tooltips": [
{
"id": 20,
"x": 0.77,
"y": 0.74,
"content": "A sheaf of wheat"
},
{
"id": 21,
"x": 0.16,
"y": 0.2,
"content": "A sheep"
}
]
}
]
Based on your sample here, It seems like you're trying to modify the state (hence the reason you're having trouble because of redux's shallow comparison). State should be regarded as immutable and everything returned in your reducers should be completely new objects. Object.assign modifies the first argument.
Try replacing return Object.assign(state, { data: newEntities })
with return Object.assign({}, state, { data: newEntities })
If you stick with this, flat data structure is not needed.
Try this with normalizr
const imgSchema = new Schema('image', { idAttribute: 'id'});
const tooltipSchema = new Schema('tooltip');
imgSchema.define({
tooltips: arrayOf(tooltipSchema)
});
const normalizedData = normalize(data, arrayOf(imgSchema));
console.log(normalizedData);
This will give you an output of
{
entities: {
image: {
1: {
id: 1,
title: 'Partride',
tooltips: [10, 11]
},
2: {
id: 2,
title: 'The Great Seal...',
tooltips: [20, 21]
}
},
tooltips: {
10: {
id: 10,
content: 'Jacky',
x: 0.56,
y: 0.59
},
...
}
},
result: [1, 2]
}
You can then save it to your redux store.
Your question is How to normalize an array of objects in the flattest way for the Redux?. I believe this is how to do it.

Use mongodb $in operator to search using array of 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.

How to display time stamp as date in higcharts?

I am building an application where it gets the data from facebook api and the graph is generated using the HighCharts.
The graph is generated successfuly, but the date i get is in the time stamp format. How can i convert it to the actual date format.
Here is the code:
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%e. %b',
year: '%b'
},
title: {
text: 'Date'
}
},
Data:
series: [{"data": [["2015-11-15T08:00:00+0000", 55], ["2015-11-16T08:00:00+0000", 45], ["2015-11-17T08:00:00+0000", 32], ["2015-11-18T08:00:00+0000", 54], ["2015-11-19T08:00:00+0000", 34], ["2015-11-20T08:00:00+0000", 1]], "name": "page_views"}, {"data": [["2015-11-15T08:00:00+0000", 23], ["2015-11-16T08:00:00+0000", 67], ["2015-11-17T08:00:00+0000", 54], ["2015-11-18T08:00:00+0000", 23], ["2015-11-19T08:00:00+0000", 64], ["2015-11-20T08:00:00+0000", 23]], "name": "page_stories"}, {"data": [["2015-11-15T08:00:00+0000", 23], ["2015-11-16T08:00:00+0000", 64], ["2015-11-17T08:00:00+0000", 34], ["2015-11-18T08:00:00+0000", 32], ["2015-11-19T08:00:00+0000", 43], ["2015-11-20T08:00:00+0000", 43]], "name": "page_fan_adds"}]
Here is the application in JSFiddle
I am not sure if we can fetch the date in any format from facebook API.
I had in the past converted the date in my code once i retrieve the same.
Below is what i had implemented with VB.NET.
DateVariable.ToString().Substring(0, 10)).ToString("dd MMM yyyy")
Hope this helps you.

How to find number of distinct values from a collection

Suppose I have a collection like:
{
"id": 1,
"name": "jonas",
},
{
"id": 2,
"name": "jonas",
},
{
"id":3,
"name": "smirk",
}
How do I get :
Number of distinct names, like in this case, 2
The distinct names, in this case, jonas and smirk ?
With some Backbone and Underscore magic, combining collection.pluck and _.uniq:
pluck collection.pluck(attribute)
Pluck an attribute from each model in the collection. Equivalent to calling map, and returning a single attribute from the iterator.
uniq _.uniq(array, [isSorted], [iterator])
Produces a duplicate-free version of the array, using === to test object equality.
[...]
var c = new Backbone.Collection([
{id: 1, name: "jonas"},
{id: 2, name: "jonas"},
{id: 3, name: "smirk"}
]);
var names = _.uniq(c.pluck('name'));
console.log(names.length);
console.log(names);
And a demo http://jsfiddle.net/nikoshr/PSFXg/

Resources