So the prisma is grouping the createdAt feild as an unique entity not by comparing the actual date of it. Like this is the answer for the group by query but all the dates in result are the same but the time is different, I can i get all the data for a perticular date
[
{ createdAt: 2023-01-20T08:15:35.846Z },
{ createdAt: 2023-01-20T08:15:35.848Z },
{ createdAt: 2023-01-20T08:15:35.847Z }
]
Related
Question is straightforward.
It's not clear to me if mongodb is ALWAYS storing dates in UTC, so in zulu(Z) zone, regardless if date string contains a zone?
I've tested with:
> db.products.updateOne( { _id: 1 }, { $set: { item: "apple" }, $setOnInsert: { dateAdded: new Date() } }, { upsert: true });
> db.products.updateOne( { _id: 2 }, { $set: { item: "jordi" }, $setOnInsert: { dateAdded: new Date("1982-05-19T14:00:00.000+05:00") } }, { upsert: true });
I detected that second date is stored a Zulu(Z):
db.products.find();
[
{
_id: 1,
dateAdded: ISODate("2022-02-02T15:40:02.457Z"),
item: 'apple'
},
{
_id: 2,
dateAdded: ISODate("1982-05-19T09:00:00.000Z"),
item: 'jordi'
}
]
Related question is, how do I need to make date range queries?
I mean, ranged dates queries have to have dates using Zulu zone?
I've tested a bit. It seems I'm able to set range queries using whichever timezone and they are transalted to Zulu:
db.products.find({ dateAdded: { $gt: ISODate("1982-05-19T13:00:00.000+05:00"), $lt: ISODate("1982-05-20T00:00:00.000Z") } });
[
{
_id: 2,
dateAdded: ISODate("1982-05-19T09:00:00.000Z"),
item: 'jordi'
}
]
The internal representation of a date doesn't refer to UTC or any other time zone, but represents a specific instant in the history of the world. Specifically, the MongoDB manual says:
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
So, a date and time of "1969-12-31T16:00:00 America/Los_Angeles" or 1970-01-01T03:00:00 Africa/Nairobi" would both be stored as the number zero, because they correspond to the arbitrary "epoch", chosen to fall at "1970-01-01T00:00 UTC".
For input, dates constructed from any of those date strings would result in the same internal value, so compare as equal.
For output, you can choose the timezone to display (e.g. which of the three strings above you want to show for an internal value of zero) by using the timezone argument to $dateToString. If you don't specify it, UTC will be used as a default, but that doesn't reflect the internal storage, just an arbitrary default for that parameter.
I have a collection of 100 records.Each record have date field like below
"created_date" : ISODate("2018-11-20T00:00:00.000Z")
I want to update each record by removing the time like below
"created_date" : "2018-11-20"
How to write a query for this kind of update
You can remove the timestamp from the date using Aggregate Query and update collection using forEach function. It may take time but it will update the collection data as per your requirement. Here is Mongo aggregation query:
db.getCollection('demo').aggregate([
{
$addFields: {
DateinString: {
$dateToString: {
format: "%Y-%m-%d",
date: "$created_date"
}
}
}
}
]).forEach(function(myDoc){
db.getCollection("demo").update({
_id: myDoc._id
}, {
$set: {
"created_date": myDoc.DateinString
}
})
})
I have a document with below fields inside a mongo collection.
{
_id: policyId_YYYYMMDDHH24MISS,
createDate: ISO DATE,
createId: VARCHAR
}
how can i append timestamp 'YYYYMMDDHH24MISS' to a field?
Expected:
{
_id: CERT00501_20160210132745,
createDate: ISO DATE,
createId: abcd1234
}
Well, you really don't need to store timestamp in MongoDB as it's default _id field do this for you.
However, if you have special needs, you can store timestamp as number in MongoDB.
// in javascript
var id = "CERT00501_"+ Date.now();
var doc = {
_id: id,
createDate: "2012-12-19T06:01:17.171Z",
createId: ""
}
You'll get a document as below:
{
_id: 'CERT00501_1457373601773',
createDate: '2012-12-19T06:01:17.171Z',
createId: ''
}
Please see more documentation about ObjectId.
Given this Job object structure, where users is an array of user documents:
{
_id: "56228b5ba851623018f88ff7",
created: "2015-10-17T17:54:35.475Z",
active: true,
workOrders: [{
startDate: "2015-10-18T05:00:00.000Z",
name: "Test1",
users: [{...}]
},{
startDate: "2015-10-20T05:00:00.000Z",
name: "Test2",
users: [{...}]
}]
}
Why does this query work:
Job.find({
'workOrders.users._id' : userId,
'created' : { '$gte' : new Date('10/17/2015'), '$lt': new Date('10/25/2015')},
'active' : true
}).exec(cb);
But this one does not:
Job.find({
'workOrders.users._id' : userId,
'workOrders.startDate' : { '$gte' : new Date('10/20/2015'), '$lt': new Date('10/25/2015')},
'active' : true
}).exec(cb);
Initial thought is that workOrder.startDate is not of type date, but it is.
Is there some reason I cannot query the nested workOrder object this way? I'm just confused why I can query the created date property on the Job document, I can query the array of nested user documents in the workOrders array, but then this last query doesn't work.
Note, there are no errors thrown, it simply returns no results. Even if I expand the date range to definitely be inclusive of all dates such as 01/01/2000 to 01/01/2016 I get nothing.
Can you replace both startDate's with:
created: {
type: Date,
default: Date.now
},
...to establish whether you can add multiple dates to a document, and that your startDate code is not incorrect?
I'm trying to run an aggregation statement in my mongo db. I have a document whose structure is (at least) as follows:
{
"_id": ObjectId,
"date": ISODate,
"keywordGroupId": NumberLong,
"ranking": NumberLong,
}
I would like to run an aggregation statement that aggregates the 'ranking' field for a given 'keywordGroupId' and a given 'date' interval.
I have been trying with the following aggregate command:
{
aggregate : "KeywordHistory",
pipeline : [
{ $match: { keywordGroupId: 75 , "$date": {$gte: ISODate("2013-01-01T00:00:00.0Z"), $lt: ISODate("2013-02-01T00:00:00.0Z")}} },
{ $group: { _id: { null }, count: { $sum: "$ranking" } } }
]
}
This command executes without errors and returns a result. If I try to change the value for the 'keywordGroupId' field, the command returns a different value, so I assume that the $match statement works for that field (NumberLong). Though, if I change the 'date' range and I specify a time interval for which I don't have any data in the database, it still returns a result (I would actually expect an empty result set). So I have to assume that the $match statement is ignoring the date interval specified.
Can anyone help me with this point?
Remove the $ prefix on the $date field of your $match:
{ $match: {
keywordGroupId: 75,
date: {$gte: ISODate("2013-01-01T00:00:00.0Z"), $lt: ISODate("2013-02-01T00:00:00.0Z")}
}},
You only use the $ prefix when the field name is used in a value, not as a key.
Sometimes ISodate does not works . so in Case if you want to match date using only "one" date the best way is:---
ex:-- Let a schema be:---
var storeOrder = new Schema({
store_name:{type:String, required:true},
date :{type:Date ,default:moment(new Date()).format('YYYY-MM-DD')},
orders : [{
vegetable : String,
quantity : Number,
price:Number
}]
});
mongoose.model('storeorder',storeOrder);
now to aggregate by matching date :--
storeOrder.aggregate([$match:{date :new Date("2016-12-26T00:00:00.000Z")} ])
**It is must to use new Date("2016-12-26T00:00:00.000z") instead of Date("2016-12-26T00:00:00.000z") because Date(your_date) !== new Date(your_date).
THANK YOU
The aggregate expects a Javascript Date Object and doesn't work otherwise.
new Date();
new Date(year, month, day);
Please note the month start with 0 and not 1 (Your January is 0 and December 11)