I am trying to create a timesheet for employees where I am saving each event(Active, Inactive) as a timestamp in MongoDB. How to calculate the active time of an employee from a given time?
{
"_id" : ObjectId("5d8c8dcca4c6010e29140e0e"),
"activityAt" : ISODate("2019-09-26T10:07:05.388+0000"),
"version" : NumberInt(1),
"deleted_at" : null,
"event" : "ACTIVE",
"employeeId" : ObjectId("5d78fba602c9265fc7ad6273"),
"created_at" : ISODate("2019-09-26T10:07:08.479+0000"),
"modified_at" : ISODate("2019-09-26T10:07:08.479+0000")
}
// ----------------------------------------------
{
"_id" : ObjectId("5d8c8dcca4c6010e29140e0f"),
"activityAt" : ISODate("2019-09-26T11:07:05.388+0000"),
"version" : NumberInt(1),
"deleted_at" : null,
"event" : "INACTIVE",
"employeeId" : ObjectId("5d78fba602c9265fc7ad6273"),
"created_at" : ISODate("2019-09-26T11:07:08.481+0000"),
"modified_at" : ISODate("2019-09-26T11:07:08.481+0000")
}
For every entry that is 'active', take the date from ISODate and subtract from it the current date (or any given time). The result will be all the active time the employee has.
In pseudocode:
active_time = 0
for entry in db:
if entry.event == 'ACTIVE':
active_time = active_time + (current_date - entry.ISODate)
return active_time
The only thing you would need to do is convert the date into something that can be subtracted/added/etc. A solution would be to convert it to hours.
Good luck!
Related
I have a collection where a unique document is created/updated everyday based on some calculations. My question is how to get last 7 days document by passing uniqueID and week to the query. And then if I pass uniqueID and months, I get to see last 12 months data grouped by year. I am new to all these kindly bear with me. My collection has the following schema:
const analyticSchema = new Schema(
{
uID: String, // unique for every document
todaysTimeStamp: Number, // epoch time for 12:00 am
animalsRescued: Number
},
{
timestamps: true
}
);
Example of my collection:
{
"_id" : ObjectId("6213fd56rg85a3b9f7996b19"),
"uID" : "20222022",
"todaysTimeStamp" : NumberInt(1645036200),
"__v" : NumberInt(0),
"createdAt" : ISODate("2022-02-17T17:55:16.616+0000"),
"animalsRescued" : NumberInt(10),
"updatedAt" : ISODate("2022-02-17T17:55:16.616+0000")
}
How to query all documents for last 12 months grouped by months such as:
{
Jan: [
{
"_id" : ObjectId("6213fd56rg85a3b9f7996b19"),
"uID" : "20222022",
"todaysTimeStamp" : 1645036200,
"__v" : 0,
"createdAt" : ISODate("2022-01-17T17:55:16.616+0000"),
"animalsRescued" : 15,
"updatedAt" : "2022-01-17T17:55:16.616+0000"
},
{
..
..
}
I have this collection in MongoDB:
{
"_id" : ObjectId("5df013b10a88910018267a89"),
"StockNo" : "33598",
"Description" : "some description",
"detections" : [
{
"lastDetectedOn" : ISODate("2020-01-29T04:36:41.191+0000"),
"lastDetectedBy" : "comp-t",
"_id" : ObjectId("5e3135f68c9e930017de8aec")
},
{
"lastDetectedOn" : ISODate("2019-12-21T18:12:06.571+0000"),
"lastDetectedBy" : "comp-n",
"_id" : ObjectId("5e3135f68c9e930017de8ae9")
},
{
"lastDetectedOn" : ISODate("2020-01-29T07:36:06.910+0000"),
"lastDetectedBy" : "comp-a",
"_id" : ObjectId("5e3135f68c9e930017de8ae7")
}
],
"createdAt" : ISODate("2019-12-10T21:52:49.788+0000"),
"updatedAt" : ISODate("2020-01-29T07:36:22.950+0000"),
"__v" : NumberInt(0)
}
I want to search by StockNo and get the name of the computer that last detected it (lastDetectedBy) only if lastDetectedOn was in the last 5 minutes with Mongoose in node.js with Express.
I also have this collection:
{
"_id" : ObjectId("5df113b10d35670018267a89"),
"InvoiceNo" : "1",
"InvoiceDate" : ISODate("2020-01-14T02:18:11.196+0000"),
"InvoiceContact : "",
"isActive" : true
},
{
"_id" : ObjectId("5df013c90a88910018267a8a"),
"InvoiceNo" : "2",
"InvoiceDate" : ISODate("2020-01-14T02:18:44.279+0000"),
"InvoiceContact : "Bob Smith",
"isActive" : true
},
{
"_id" : ObjectId("5e3096bb8c9e930017dc6e20"),
"InvoiceNo" : "3",
"InvoiceDate" : ISODate("2020-01-14T02:19:50.155+0000"),
"InvoiceContact : "",
"isActive" : true
}
And I want to update all the documents with empty InvoiceContact which has been issued in the last 30 seconds (or any date range between now and sometime in the past) with isActive equals true to isActive equals false. So for example, the first record has been issued in the last 30 seconds without InvoiceContact and isActive is true so this must be updated but the next two records will remain untouched for different reasons, the second record has InvoiceContact and the third record is out of range.
First Part
var mins5 = new Date(ISODate() - 1000* 60 * 5 )
db.getCollection('user').find({$and:[
{ "StockNo":"33598"},
{"detections.lastDetectedOn" : { $gte : mins5 }}
]})
.map(function(list){
var results = [];
list.detections.forEach(function (detections){
if(detections.lastDetectedOn > mins5){
results.push(detections.lastDetectedBy);
}
})
return results;
});
Second Part could be solved by a similar query using update instead of find.
This is document in a Data base.
{
"_id" : "E08AF4BD-5539-497E-B8DA-51AD1A14F7A7",
"checkinCategory" : "Transient",
"checkinData" : {
"time" : ISODate("2018-01-19T09:52:03.708+05:30"),
"dateFormat" : "2018-1-19",
"addedOn" : ISODate("2018-01-19T09:52:08.514+05:30"),
"checkinId" : "E08AF4BD-5539-497E-B8DA-51AD1A14F7A7"
}
}
I am using aggregate Query and trying to get hour from checkinData.time.
db.checkins.aggregate(
{"$match":
{"_id":"E08AF4BD-5539-497E-B8DA-51AD1A14F7A7"}},
{"$project":{"checkinData.time":1,
hour: { $hour: ("$checkinData.time")}
}}
})
Mongo returns
{
"_id" : "E08AF4BD-5539-497E-B8DA-51AD1A14F7A7",
"checkinData" : {
"time" : ISODate("2018-01-19T09:52:03.708+05:30")
},
"hour" : 4
}
I want to get hour in ISO format like 9. Plz sugget me where am I doing wrong..Thanks!
I want to perform a search in mongodb and nodejs which will return the count of ids that I will provide.
my collection is a log table
{
"_id" : ObjectId("5836d0f7f8462cbc6d0caffc"),
"DeviceId" : "abcd1234",
"AppType" : "web",
"UserId" : "5836cb01f8462cbc6d0caff8",
"ArticleId" : "5836cb01f8462cbc6d0caff8",
"Timestamp" : ISODate("2016-11-24T11:37:27.851Z")
},
{
"_id" : ObjectId("5836dba8a2943528448a3050"),
"DeviceId" : null,
"AppType" : null,
"UserId" : null,
"ArticleId" : 5836e493f2acbd1d34648e78,
"Timestamp" : ISODate("2016-11-24T12:23:04.484Z")
},
{
"_id" : ObjectId("5836e445c3b43429b4810ad4"),
"DeviceId" : null,
"AppType" : null,
"UserId" : null,
"ArticleId" : 5836d0f7f8462cbc6d0caffc,
"Timestamp" : ISODate("2016-11-24T12:59:49.820Z")
},
{
"_id" : ObjectId("5836e493f2acbd1d34648e78"),
"DeviceId" : null,
"AppType" : null,
"UserId" : null,
"ArticleId" : 5836d0f7f8462cbc6d0caffc,
"Timestamp" : ISODate("2016-11-24T13:01:07.030Z")
}
and so on...
my search string will be, search need to be performed on ArticleId
{"5836d0f7f8462cbc6d0caffc",
"5836e493f2acbd1d34648e78",
"5836dba8a2943528448a3050"}
and I want a result set like below
{
"1":{ArticleId:"5836d0f7f8462cbc6d0caffc", count:2},
"2":[ArticleId:"5836e493f2acbd1d34648e78", count:9},
"3":[ArticleId:"5836dba8a2943528448a3050", count:35}
}
Can any one please provide me the query, thanks in advance
I don't know if you realize, but all of your counts will be 1.
That's because every _id in Mongo is unique so if it's there then it's there only once.
Also, the output that you want is invalid:
{ 1:{_id:ObjectId("5836d0f7f8462cbc6d0caffc"), count:2},
2:{ObjectId("5836e493f2acbd1d34648e78"), count:9},
3:{ObjectId("5836dba8a2943528448a3050"), count:35}}
It's not valid JSON, not valid JavaScript, not valid Hjson, not valid JSON5, not valid anything - so you will never be able to get that result no matter what you do. (And also, if you change the expected output, every count will still be 1 so fixing the format is pointless anyway.)
Hello all I got stuck somewhere, I am working on mongodb with node.js where my collection data deleted automatically after 1 year on certain date and I want to stop that permanently how can I do that ? I have checked the available material on google but didn't got much success please help me friends ...
I have checked the index in one of my collection and it is showing data like this . Can you please tell me its is having TTL index or not
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "firstfive.teachers"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "firstname_lastname_text",
"weights" : {
"firstName" : 1,
"lastName" : 1
},
"default_language" : "english",
"language_override" : "language",
"ns" : "firstfive.teachers",
"textIndexVersion" : 2
}
]
most likely you have TTL (time to limit) index defined on collection you're working with (https://docs.mongodb.com/v3.2/core/index-ttl/)
yu can check it by running db.your_collection.getIndexes() (it will be one with expireAfterSeconds) in mongo shell.
as any other index it can be removed - but do it carefully, apparently someone did it deliberately