I'm new to firebase, actually I'm trying to load some data based on timestamp and retrieve between two timestamp using startat and endat. Data is pushed through Python.
The data is as shown in the sample screenshot
sample data
The data path popping-fire-7751.firebaseio.com/device488
activevolt: 396
avgPowerFactor: 0.95
avgThdi: 7.5
cubetemp: 37
datetime: "2016-02-08 15:16:32"
timestamp: 1454924792
totalPowerGen: 34
I'm pushing data through python setting the priority as timestamp.
When I try to filter as shown here it returns null. But without startAt and endAt it shows all the values.
http://jsfiddle.net/casperkamal/EZUtP/64/
var startTime = 1454924792;
var endTime = 1454924798;
new Firebase("https://popping-fire-7751.firebaseio.com/")
.startAt(startTime)
.endAt(endTime)
.once('value', show);
can anybody help me on what I'm doing wrong ?
You're trying to skip a level in your JSON tree:
var startTime = 1454924792;
var endTime = 1454924798;
new Firebase("https://popping-fire-7751.firebaseio.com/")
.child('device488')
.startAt(startTime)
.endAt(endTime)
.once('value', show);
I highly recommend that you don't depend on the implicit priority anymore and instead just filter using orderByChild()
new Firebase("https://popping-fire-7751.firebaseio.com/")
.child('device488')
.orderByChild('timestamp')
.startAt(startTime)
.endAt(endTime)
.once('value', show);
You'll need to add an index to your Security Rules:
{
"rules": {
"device488": {
".indexOn": ["timestamp"]
}
}
}
But the more explicit behavior is well worth the effort of adding this.
Related
I'm trying to work with some data that has close bindings to dates. As shown in the snippet down below I'm trying to find the index of the closest day to today. I'm using date-fns utility library for this job. While I'm trying to log the closestIndex from it works fine and got an output in the terminal but while I'm trying to utilize the value of closestIndex I got an error that says closestIndex is undefined.
Any Ideas would be very appreciated.
import * as dateFns from 'date-fns';
const today = new Date().getTime();
const dates = [
2022-04-10T14:07:12.276Z,
2022-04-10T14:07:06.967Z,
2022-04-10T14:07:04.663Z,
2022-04-10T14:07:03.040Z,
2022-04-10T14:07:01.420Z,
2022-04-10T14:06:59.869Z,
2022-04-10T14:06:53.223Z
]
const closestIndex = dateFns.closestTo(today, dates);
console.log(closestIndex); // => 0
console.log(dates[closestIndex]); // => undefined could not be used as index value
You should use real Date objects inside your array (rather than ISO-8601 values which are not even quoted) and use closestIndexTo instead of closestTo (which will return the Date value itself rather than its index in the array)
const today = new Date().getTime();
const dates = [
new Date('2022-04-10T14:07:12.276Z'),
new Date('2022-04-10T14:07:06.967Z'),
new Date('2022-04-10T14:07:04.663Z'),
new Date('2022-04-10T14:07:03.040Z'),
new Date('2022-04-10T14:07:01.420Z'),
new Date('2022-04-10T14:06:59.869Z'),
new Date('2022-04-10T14:06:53.223Z')
]
const closestIndex = dateFns.closestIndexTo(today, dates);
console.log(closestIndex); // => 0
console.log(dates[closestIndex]); // => "2022-04-10T14:07:12.276Z"
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"></script>
Looking for assistance in replicating the following mssql operation in sequelize. We have a table with a valid_from and valid_to columns and I need to get todays current date and only return the records that are fall based on that.
I am looking to utilize the .findByPK(), or .findAll() methods in sequelize
SELECT * FROM [table] WHERE GETUTCDATE() BETWEEN f.valid_from AND f.valid_to
I have found the following posts and items with no luck. As they seem to specify two di9ffernt dates between the same column. I need to compare the current UTCDATE between two different columns
Sequelize Query to find all records that falls in between date range
Sequelize query - compare dates in two columns
I was able to simulate but would still like to know if anyone knows how to do the same thing using the between operator
const now = new Date();
return await models.Form.findAll({
where: {
valid_from: {
[Op.lte]: Sequelize.cast(now, 'DATETIMEOFFSET')
},
[Op.and]: {
validTo: {
[Op.gte]: Sequelize.cast(now, 'DATETIMEOFFSET')
}
}
}
});
You can write this query using sequelize.where() to generate the WHERE statement for the BETWEEN operation. For the current timestamp you need to call GETUTCDATE() which we can do via sequelize.fn().
const forms = await models.Form.findAll({
where: {
// create where condition for GETUTCDATE()
sequelize.where(sequelize.fn('GETUTCDATE'), {
// BETWEEN valid_from AND validTo
[Op.between]: [
sequelize.col('valid_from'),
sequelize.col('validTo')
],
}),
},
});
This will generate SQL like:
SELECT * FROM `form`
WHERE GETUTCDATE() BETWEEN `valid_from` AND `validTo`
note that you have underscored valid_from and camelCase validTo in your example.
I found this method, with which I have ordered the hours what I store in my records, I wanted them to be sorted from highest to lowest, but when I test the code, I notice that only two values of the array are compared, which are the first registers.
I've seen other methods of comparison and the logic is the same, what am I doing wrong? I group the messages per user, using the id of the user as key of the array , then I save the rest of data. I do this for retrieve the current messages, since I want show a list of the last currently messages sent.
This is the code:
var ref = new Firebase('https://chatfbexample.firebaseio.com/all-messages');
ref.on("value", function (snapshot) {
var Messages = [];
var value = 0;
snapshot.forEach(function (snap) {
value = snap.val().id;
fecha = snap.val().id;
Messages[value] = [];
Messages[value]['fecha'] = snap.val().fechahora; //I receive a date with the format HH:MM:SS
Messages[value]['texto'] = snap.val().texto;
});
function compare(a, b) {
var time1 = a['fecha'].replace(/:/gi, '1');
var time2 = b['fecha'].replace(/:/gi, '1');
var data1 = parseInt(time1);
var data2 = parseInt(time2);
// alert(time1);
if (data1 > data2)
return -1;
if (data1 < data2)
return 1;
return 0;
}
Messages.sort(compare);
for (var i in Messages) {
console.log("Hour: " + Messages[i]['fecha'] + ' ' + ' Message: ' + Messages[i]['texto']);
}
});
the result is something like this
Hour: 12:11:13 Message: whats'up?
Hour: 11:38:44 Message: p
Hour: 11:49:01 Message: hey?
the second and the third messages are not being compared
an image of my Firebase database
First off it probably would be better for you to save a timestamp from Firebase instead of an actual time, which was probably created on the client side, inside your database. This way you could easily let Firebase handle the sorting.
You would need to use firebase.database.ServerValue.TIMESTAMP to save the exact time at which Firebase received your message inside fechahora.
Following you could simply query all of your messages ordered by their child fechahora.
ref.orderByChild("fechahora").on("value", function (snapshot) {
//Do stuff with your already sorted data
}
If you want your query to have a better performance set the .indexOn property of your node containing all the messages inside your Firebase Database rules to fechahora.
Bonus: If you want your data to be ordered newest to oldest instead of oldest to newest, you just need to use the negative value of the timestamp created by Firebase.
Instead of ordering client-side, you'll be better off asking the server to sort the messages:
ref.orderByChild("fechahora").on(...
Make sure that you define the proper index in your Firebase security rules:
{
"rules": {
"all-messages": {
".indexOn": "fechahora"
}
}
}
Without that the sorting will still work, but will happen on the client instead of the server.
In MongoDB collection I have 3 objects. I need to update one variable (date type) in each object. The main task is to increment the date of the objects. For example: all objects have the same variable:
"Time1" : ISODate("2016-01-12T21:37:46.738Z")
My problem is to update the first object with the current date, manually I do it in this way:
$db.getCollection('my.data')({'_id':ObjectId("52e637fca92cf1ec6a73c1e8")}, {$currentDate: {Time1: true}})
The next is to increase the date of the second object by 1 day, I mean to update it with tomorrow date. I couldn't do it through the shell, because $inc doesn't work with Date type.
So, I am lost with javascript
I found how to get it with java script but I don't know how to collect all if this in one script.
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
Thank you for your help.
You could use the $set operator for the other fields, together the the $currentDate operator all within the update object:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
var dayAfterTomorrow = new Date();
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate()+2);
db.getCollection("my.data").update(
{ "_id": ObjectId("52e637fca92cf1ec6a73c1e8") },
{
"$currentDate": { "Time1": true },
"$set": {
"Time2": tomorrow,
"Time3": dayAfterTomorrow
}
}
)
I've created a Collection containing 1 million documents, and I'm trying to select 50000 of these records based on the ObjectID, and update one of the values (i'm working in Mongo shell, running on Ubuntu).
Is it possible to define a 50000 document range? It doesn't matter which documents are included in the 50000 range, I simply want to ringfence a definite number of records and run an update operation using the primary id so that I can measure the performance time.
The code I've tried to run doesn't work:
use Assignment
var _start = new Date()
db.FlightsDate.update({$set:{Airtime: 8888}}).limit(50000).hint({_id:1});
var _end = new Date();
print("Time to Bulk Update AirTime key for 50000 documents… " + ((_end _start)/1000));
...i'm gathering that MongoDB needs me to include a query in the command to specify which docs are to be updated (I now understand from reading other posts that .limit won't constrain the number of records than an .update writes to).
Please can anyone advise a method that'll enable me to define the number of records to be updated?
Grateful for advice.
R,
Jon
If you are simply looking for a "range" that covers 50,000 of the documents in the collection then your best approach is to query and find the "starting" and "ending" documents of your range first. Then apply that "range" specification to your update.
var start_id = db.FlightsDate.find({}).limit(1).toArray()[0]._id;
var end_id = db.FlightsDate.find({}).skip(49999).limit(1).toArray()[0]._id;
var _start = new Date();
db.FlightsDate.update(
{ "_id": { "$gte": start_id, "$lte": end_id } },
{ "$set"; { "Airtime": 8888 } },
{ "multi": true }
);
var _end = new Date();
( _end - _start )/1000;
If you then wanted the next 50,000 in an additional range then :
var start_id = db.FlightsDate.find(
{ "_id": { "$gt": end_id } }
).limit(1).toArray()[0]._id;
var end_id = db.FlightsDate.find(
{ "_id": { "$gt": end_id } }
).skip(49999).limit(1).toArray()[0]._id;
And do it all again.
The point is you need to know where to "start" and when to "end" within a range to limit your update to just 50,000 documents without any other criteria to do so.
Also note the usage of "multi" in the update method there. By default, .update() does not "update" any more than one document, essentially being the first match. So what you mean to do is update "all documents in the range" and that is why you need to apply "multi" here.