Just can't get this seemingly simple query to work. All I want to do is match records that have a specific date range (the range could be 1 for the same date). Any insight is appreciated. I have verified the collection has documents with date = "2015-11-23T09:00:00.000Z".
db.getCollection('MyCollection').find(
{ "$and" :
[ { "name" : { "$in" : [ "Joe", "Jane"]}} ,
{ "date" : { "$gte" : { "$date" : "2015-11-23T09:00:00.000Z"} , "$lte" : { "$date" : "2015-11-23T09:00:00.000Z"}}}
]}
)
frostbite,
Just change your above query to:
db.getCollection('MyCollection').find(
{
"$and" :
[
{ "name" : { "$in" : [ "Joe", "Jane"]}} ,
{ "date" : {
"$gte" :ISODate("2015-11-23T09:00:00.000Z"),
"$lte":ISODate("2015-11-23T09:00:00.000Z")
}
]
})
Related
I have the following document:
{
id: "myId",
boundedPlan: {
plannedWeeks: [
0 : {
weekStartDate: date
weekEndDate: date
plannedDays: []
},
...
]
},
unboundedPlan: {
plannedWeeks: [
0 : {
weekStartDate: date
weekEndDate: date
plannedDays: []
},
...
]
}
}
This plan represent some number of weeks in the future. The plan has a bounded or unbounded plan .
(I have the same structure on two different fields, because in the code they correspond to two different classes with different behavior).
I now have to do the following query.
"Get the current plan week given a date"
I wrote the following pipeline:
[
{ "$match" : { "ownerId" : "defaultOwnerId"}},
{ "$project" : {
"boundedPlan" : 1,
"unboundedPlan" : 1,
"plannedWeeks" : {
"$cond" : {
"if" : { "$ne" : ["$boundedPlan", null]}, "then" : "$boundedPlan.plannedWeeks",
"else" : "$unboundedPlan.plannedWeeks"}
}
}
},
{ "$match" : {
"boundedPlan.plannedWeeks" : {
"$elemMatch" : { "weekStart" : { "$lte" : { "$date" : "2021-03-10T00:00:00Z"}}, "weekEnd" : { "$gte" : { "$date" : "2021-03-10T00:00:00Z"}}}},
"$or" : [{
"unboundedPlan.plannedWeeks" : {
"$elemMatch" : { "weekStart" : { "$lte" : { "$date" : "2021-03-10T00:00:00Z"}}, "weekEnd" : { "$gte" : { "$date" : "2021-03-10T00:00:00Z"}}}}
}]}
}
]
The problem is the following:
knowing that Im operating over a plan with an unbounded plan and explicitly setting the second match :
"$match" : {
"unboundedPlan.plannedWeeks" : {
"$elemMatch" : { "weekStart" : { "$lte" : { "$date" : "2021-03-10T00:00:00Z"}}, "weekEnd" : { "$gte" : { "$date" : "2021-03-10T00:00:00Z"}}}},
}
works.
of course I dont know if the plan is from the unbounded or bounded field, so I tried to add the or operator, which causes no selection at all.
Is there something Im missing?
(working with spring data mongo)
Thank you
Ok, found out... I was using the OrOperator class from spring data mongo wrongly:
new Criteria("field1").orOperator(new Criteria("field2"))
is not the same as
new Criteria().orOperator(new Criteria("field1"), new Criteria("field2")
Hi I have a Mongo aggregation:
[
{
"$match" : {
"dateTime" : {
"$gte" : ISODate("2017-01-01T00:00:00.000+0000"),
"$lt" : ISODate("2018-01-01T00:00:00.000+0000")
}
}
},
{
"$group" : {
"_id" : "dateTime",
"totals" : {
"$sum" : "$payment.totalAmount"
},
"count" : {
"$sum" : 1.0
}
}
}
],
{
"allowDiskUse" : false
}
);
This works fine. It aggregates, and sums by date range I supplied and I get an output as follows.
{
"_id" : "dateTime",
"totals" : 2625293.825017198,
"count" : 12038.0
}
However, I also want to further refine the groupings.
I have a field called 'companyId' and I want to calculate the sum and count by each company Id for the given time range.
I would like to get an output similar to this, where I get a sum and count for each company ID in the date range I queried, not just a sum/count of all the data:
[
{
"companyId" : "Acme Co",
"totals" : 2625293.825017198,
"count" : 12038.0
},
{
"companyId" : "Beta Co",
"totals" : 162593.82198,
"count" : 138.0
},
{
"companyId" : "Cel Co",
"totals" : 593.82,
"count" : 38.0
}
]
How do I do this? I have not been able to find a good example online.
Thanks
I am new in mongodb, my mysql query is:
select count(_id) as numberOfRecord,createdDate,orderId
from CakeOrder
where createdDate >= '2019-08-12' and createdDate <= '2019-10-12'
group by createdDate
order by createdDate desc
How I will convert this query to mongodb?
Date format is "createdDate" : ISODate("2019-10-12T07:12:36.390Z")
You can do this with MongoDB Aggregation Framework
here is mongo shell command for you:
db.getCollection("myCollection").aggregate(
[
{
"$match" : {
"$and" : [
{
"createDate" : {
"$gte" : ISODate("2019-01-01T00:00:00.000+0000")
}
},
{
"createDate" : {
"$lte" : ISODate("2019-01-01T00:00:00.000+0000")
}
}
]
}
},
{
"$group" : {
"_id" : "$createDate",
"count" : {
"$sum" : 1
}
}
},
{
"$sort" : {
"_id" : -1
}
}
]
);
My collection is below:
{
"_id" : ObjectId("5d88953e7a20304f3c76f264"),
"purchaseId" : "5d88953e7a20304f3c76f263",
"partId" : "5d2584b5d24e8b3f0885737b",
"SellPrice" : "1885",
"TotalAmnt" : "2667.24",
"Tax" : "28",
"Discount" : "8708",
"Quantity" : "2",
"Price" : "1985",
"Per" : "PCS",
"DiscountAmnt" : "32.815",
"Sgst" : "373.41",
"Cgst" : "373.41",
"Igst" : "746.83",
"TotalAmntData" : "3414.06",
"salecostprice" : "1707",
"finalqtydata" : "4",
"part" : "542746990101",
"purchaseStatus" : "0",
"datetime" : "2019-09-23",
"__v" : 0
}
I try this query
db.purchaseitems.find({ "part" : "542746990101":{ $gte: "datetime" : "2019-09-23", $lte: "datetime" : "2019-09-23")}});
Please use following line
const lastUpdateDateCond = { datetime: { $gt: new Date('2019-09-23) }
Please use new keyword in date filter
Here is the query assuming you insist on using string to hold date values...
db.purchaseitems.find({
$and:
[
{ "partId" : "5d2584b5d24e8b3f0885737b" },
{ "datetime": { "$gte": "2019-09-23" } },
{ "datetime": { "$lte": "2019-09-23" } }
]
})
I am new to MongoDB and I've been struggling to get a specific query to work without any luck.
I have a collection with millions of documents having a date and an amount, I want to get the aggregations for specific periods of time.
For example, I want to get the count, amount summations for the periods between 1/1/2015 - 15/1/2015 and between 1/2/2015 - 15/2/2015
A sample collection is
{ "_id" : "148404972864202083547392254", "account" : "3600", "amount" : 50, "date" : ISODate("2017-01-01T12:02:08.642Z")}
{ "_id" : "148404972864202085437392254", "account" : "3600", "amount" : 50, "date" : ISODate("2017-01-03T12:02:08.642Z")}
{ "_id" : "148404372864202083547392254", "account" : "3600", "amount" : 70, "date" : ISODate("2017-01-09T12:02:08.642Z")}
{ "_id" : "148404972864202083547342254", "account" : "3600", "amount" : 150, "date" : ISODate("2017-01-22T12:02:08.642Z")}
{ "_id" : "148404922864202083547392254", "account" : "3600", "amount" : 200, "date" : ISODate("2017-02-02T12:02:08.642Z")}
{ "_id" : "148404972155502083547392254", "account" : "3600", "amount" : 30, "date" : ISODate("2017-02-7T12:02:08.642Z")}
{ "_id" : "148404972864202122254732254", "account" : "3600", "amount" : 10, "date" : ISODate("2017-02-10T12:02:08.642Z")}
for date ranges between 1/1/2017 - 10/10/2017 and 1/2/2017 - 10/2/2017 the output would be like this:
1/1/2017 - 10/1/2017 - count =3, amount summation: 170
10/2/2017 - 15/2/2017 - count =2, amount summation: 40
Is it possible to work with such different date ranges? The code would be in Java, but as an example in mongo, can someone please help me?
There must be a more elegant solution than this. Anyways you can wrap it into a function and generalize date related arguments.
First, you need to make a projection at the same time deciding into which range an item goes (note the huge $switch expression). By default, an item goes into 'null' range.
Then, you filter out results that didn't match your criteria (i.e. range != null).
The very last step is to group items by the range and make all needed calculations.
db.items.aggregate([
{ $project : {
amount : true,
account : true,
date : true,
range : {
$switch : {
branches : [
{
case : {
$and : [
{ $gte : [ "$date", ISODate("2017-01-01T00:00:00.000Z") ] },
{ $lt : [ "$date", ISODate("2017-01-10T00:00:00.000Z") ] }
]
},
then : { $concat : [
{ $dateToString: { format: "%d/%m/%Y", date: ISODate("2017-01-01T00:00:00.000Z") } },
{ $literal : " - " },
{ $dateToString: { format: "%d/%m/%Y", date: ISODate("2017-01-10T00:00:00.000Z") } }
] }
},
{
case : {
$and : [
{ $gte : [ "$date", ISODate("2017-02-01T00:00:00.000Z") ] },
{ $lt : [ "$date", ISODate("2017-02-10T00:00:00.000Z") ] }
]
},
then : { $concat : [
{ $dateToString: { format: "%d/%m/%Y", date: ISODate("2017-02-01T00:00:00.000Z") } },
{ $literal : " - " },
{ $dateToString: { format: "%d/%m/%Y", date: ISODate("2017-02-10T00:00:00.000Z") } }
] }
}
],
default : null
}
}
} },
{ $match : { range : { $ne : null } } },
{ $group : {
_id : "$range",
count : { $sum : 1 },
"amount summation" : { $sum : "$amount" }
} }
])
Based on your data it will give the following results*:
{ "_id" : "01/02/2017 - 10/02/2017", "count" : 2, "amount summation" : 230 }
{ "_id" : "01/01/2017 - 10/01/2017", "count" : 3, "amount summation" : 170 }
*I believe you have few typos in your questions, that's why the data look different.