MongoDB exception Resolving [duplicate] - mongodb

I am getting an issue for running the following aggregate query:
db.snippets.aggregate([ { '$project': { month: { '$month': '$created_at' }} } ])
The error message for the same is:
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0 } : aggregate failed
How do I get around this issue? I found a related question: MongoDB: can't convert from BSON type EOO to Date.
But it doesn't tell me how to get things done.

You likely have one or more docs with a created_at value that's not a BSON Date and you'll need to fix that by converting those values to Date or removing them.
You can find those docs with a $not query that uses the $type operator like:
db.snippets.find({created_at: {$not: {$type: 9}}})
If the created_at values are date strings, you can find the docs that need updating and then update them in the shell using code like:
db.snippets.find({created_at: {$not: {$type: 9}}}).forEach(function(doc) {
// Convert created_at to a Date
doc.created_at = new Date(doc.created_at);
db.snippets.save(doc);
})

try this one, its help for me above problem.
db.snippets.aggregate([{
'$project': {
month: { $substr: ["$created_at", 5, 2] }
}
}]);
above code get month wise
data is entered into the database in ISO format which can then be easily worked with.

In some situations, some documents are supposed to have empty Date fields. In those cases, you could try this (using your example):
db.snippets.aggregate([ { '$project': { month:
{ $cond: [{ $ifNull: ['$created_at', 0] }, { $month: '$created_at' }, -1] }} } ])
In this example, we would get -1 in the cases whenever no field '$created_at' is found. For all the other cases, we would get the Date month.

I had a related issue, but in my case the Date fields were the members of an array, so the error was "can't convert BSON type Object to Date".
I needed to get the day of week from the dates in the possibleTripDateTimes array.
Sample document:
{
"possibleTripDateTimes" : [
{
"tripDateTime" : ISODate("2015-08-01T06:00:00.000-0700")
}
]
}
The fix was simply to use dot notation to address the array member fields.
db.trips.aggregate([
{
$project: {
departTime: {
$map: {
input: "$possibleTripDateTimes.tripDateTime",
as: "dateTime",
in: { $dayOfWeek: "$$dateTime" }
}
}
}
}
]
);
I hope this helps someone who also gets zero search results on the "BSON type Object" search

I had the same problem, I figured that the date field is missing for some of the documents causing the conversion to fail. I just added a match clause to filter these out. But ofcourse i am investigating on my app side why they are not being populated.
db.snippets.aggregate([
{
'$match': {
'created_at': {
"$exists": true
}
}
},
{
'$project': {
month: {
'$month': '$created_at'
}
}
}
])

I had a similar problem, and solved it checking if the date existed.
db.users.aggregate([
{$project:{day: { $cond: ["$bd", { $dayOfMonth: "$bd" }, -1] },
month: { $cond: ["$bd", { $month: "$bd" }, -1] },
year: { $cond: ["$bd", { $year: "$bd" }, -1] }
}},
{$match:{"month":1, "day":15}}
])
My date field is bd and with that match I'm getting all users that have their birthday on January 15th.

This error can also appear if you have incorrectly named your properties in your aggregation relative to what they are in your database.
For example my code was
$group: {
_id: {$week: "$projects.timeStamp"},
total: { $sum: "$projects.hours" }
}
But I hadn't camelcased timestamp in my database so simply modifying to projects.timestamp fixed it.

First, you need to check whether the data type is in ISODate. IF not you can change the data type as the following example.
db.collectionName.find().forEach(function(each_object_from_collection){each_object_from_collection.your_date_field=new ISODate(each_object_from_collection.your_date_field);db.collectionName.save(each_object_from_collection);})
Now you can find it in two ways
db.collectionName.find({ $expr: {$eq: [{ $year: "$your_date_field" }, 2017]}});
Or by aggregation
db.collectionName.aggregate([{$project: {field1_you_need_in_result: 1,field12_you_need_in_result: 1,your_year_variable: {$year: '$your_date_field'}, your_month_variable: {$month: '$your_date_field'}}},{$match: {your_year_variable:2017, your_month_variable: 3}}])

First, you can identify the particular field which is causing an issue as follows:
db.collectionName.find( { 'dateField' : { $type : 2 } } )
The above line checks and finds all the documents with field name 'dateField' having type as String(referred as $type - 2).
Once it is identified and verified, we can modify those records as follows:
db.collectionName.find( { 'dateField' : { $type : 2 } } ).forEach( function (x) {
x.dateField = new ISODate(x.dateField);
db.collectionName.save(x);
});

In my case I had to use "$toDate" and it worked:
db.snippets.aggregate([ { '$project': { month: { '$month': {$toDate: '$created_at'} }} } ])

Related

MongoDB - Dates between using $match

So I try to use MongoDB $match to get data between 2 dates, but it turns out that the data is not returning a.k.a empty here. What it looks like:
db.collection.aggregate([
{
$match: {
date: {
$gte: new Date("2022-10-23"),
$lt: new Date("2022-10-25"),
},
}
},
{
$group: {
_id: "$title",
title: {
$first: "$title"
},
answer: {
$push: {
username: "$username",
date: "$date",
formId: "$formId",
answer: "$answer"
}
}
}
},
])
Here is the data that I try to run on the Mongo playground:
https://mongoplayground.net/p/jKx_5kZnJNz
I think there is no error with my code anymore... but why it gives an empty return.
Migrate the comment to the answer post for the complete explanation.
Issue 1
The document contains the date field as a string type while you are trying to compare with Date which leads to incorrect output.
Ensure that you are comparing both values in the exact type.
Either that migrate the date value to Date type or
converting the date field to Date type in the query via $toDate.
{
$match: {
$expr: {
$and: [
{
$gte: [
{
$toDate: "$date"
},
new Date("2022-10-23")
]
},
{
$lt: [
{
$toDate: "$date"
},
new Date("2022-10-25")
]
}
]
}
}
}
Issue 2
Since you are using $lt ($lt: new Date("2022-10-25")), it won't include the documents with date: new Date("2022-10-25").
For inclusive end date, you shall use $lte.
Demo # Mongo Playground

MongoDB $type check on a field inside $cond

I'm new to MongoDB and my requirement is to convert a string date to date. But that particular field is sometimes in date format sometimes in string format.
Effectively, If the date is in string format I want to convert it to date else leave as it is.
Sample data:
paymentDate:2021-11-19T05:36:32.596+00:00
paymentDate:'2021-11-19T05:36:32.596+00:00'
My attempt is
{
convertedDate: {
$cond: {
if:
{'$eq': [{$type:"$paymentDate"},9]},
then:"$newField",
else:{
$dateFromString: {
dateString: '$paymentDate'
}
}
}
}
}
You're almost to the answer. Specify the compare value in $type as "date".
db.collection.find({},
{
convertedDate: {
$cond: {
if: {
"$eq": [
{
$type: "$paymentDate"
},
"date"
]
},
then: "$paymentDate",
else: {
$dateFromString: {
dateString: "$paymentDate"
}
}
}
}
})
Sample Mongo Playground
References
Available type | $type
If you are using MongoDB 4.2+, you may simply use $toDate to convert your field in an update with aggregation pipeline operation.
db.collection.update({},
[
{
"$set": {
"paymentDate": {
"$toDate": "$paymentDate"
}
}
}
])
Here is the Mongo playground for your reference.

MongoDB date math with aggregation variable

I'm trying to build an aggregation of things that haven't reported in by some interval (heartbeat) - I need to calculate a value based on a stored heartbeat:
db.things.aggregate([
{$project: {"lastmsg":1, "props.settings":1}},
{$unwind: "$props.settings"},
{$project: {
_id:0,
"lastmsg": "$lastmsg",
"heartbeat": {$multiply: [{$toInt: "$props.settings.heartbeat"},2000]},
"now": new Date(), "subtracted": new Date(new Date().getTime()- "$heartbeat")
}
}
])
Result returned is like this:
{ "lastmsg" : ISODate("2020-04-23T12:41:37.667Z"), "heartbeat" : 240000, "now" : ISODate("2020-05-14T16:26:11.824Z"), "subtracted" : ISODate("1970-01-01T00:00:00Z") }
{ "lastmsg" : ISODate("2020-05-14T16:24:24.228Z"), "heartbeat" : 240000, "now" : ISODate("2020-05-14T16:26:11.824Z"), "subtracted" : ISODate("1970-01-01T00:00:00Z") }
The "subtracted" projection is not doing the date math as expected. I can plug in a specific number and it works but this defeats the purpose...
As a last step I will match to see what of these things hasn't checked in within the interval of heartbeat:
{ $match: { "lastmsg":{$gte: "$subtracted")}
Any help would be greatly appreciated...
I don't know how your data is like (you should post your data to help), but I think this can solve the problem.
You can use the $$NOW variable, that returns the current date in ISODate format.
Test data:
[
{
"lastmsg": ISODate("2020-04-23T12:41:37.667Z"),
"heartbeat": 240000
},
{
"lastmsg": ISODate("2020-05-14T16:24:24.228Z"),
"heartbeat": 240000
}
]
Query:
db.collection.aggregate([
{
$addFields: {
"now": "$$NOW",
"subtracted": {
$subtract: [
"$$NOW",
"$heartbeat"
]
}
}
},
{
$match: {
"lastmg": {
$gte: "$subtracted"
}
}
}
])

Mongo query using aggregation for dates

In the following query I'm trying to find entries in my articles collection made in the last week, sorted by the number of votes on that article. The $match doesn't seem to work(maybe I dont know how to use it). The following query works perfectly, so its not a date format issue,
db.articles.find(timestamp:{
'$lte':new Date(),
'$gte':new Date(ISODate().getTime()-7*1000*86400)}
})
But this one doesn't fetch any results. Without the $match it also fetches the required results(articles sorted by votecount).
db.articles.aggregate([
{
$project:{
_id:1,
numVotes:{$subtract:[{$size:"$votes.up"},{$size:"$votes.down"}]}}
},
{
$sort:{numVotes:-1}
},
{
$match:{
timestamp:{
'$lte':new Date(),
'$gte':new Date(ISODate().getTime()-7*1000*86400)}
}
}
])
You are trying to match at the end of your pipeline, which supposes you have projected timestamp field, and you haven't done that.
I believe what you want is to filter data before aggregation, so you should place match at the top of your aggregation array.
Try this:
db.articles.aggregate([{
$match: {
timestamp: {
'$lte': new Date(),
'$gte': new Date(ISODate().getTime() - 7 * 1000 * 86400)
}
}
}, {
$project: {
_id: 1,
numVotes: {
$subtract: [{
$size: "$votes.up"
}, {
$size: "$votes.down"
}]
}
}
}, {
$sort: {
numVotes: -1
}
}])

MongoDB $gt not working

Below query gives me no results, though there are fields matching the geo location and time stamp.
I have tried changing the $gt to $ne and it returns the sum of all $values.
I have even tried checking timeStamp greater than 0. still no result. Here timestamp is a double datatype.
db.csv2.aggregate([{
$match: {
$and: [{
loc: {
$geoWithin: {
$box: [
[-128.232422, 26.382028],
[-109.6875, 43.624147]
]
}
}
}, {
timeStamp: {
$gt: 1432201420790
}
}]
}
}, {
$group: {
_id: null,
total: {
$sum: "$value"
}
}
}]);
The error given to you is explicit. Read the doc to see how to implement the $gt aggregation function : documentation
Basically, the condition is an array
{ $gt: ["$timeStamp", 1432201420790] }
[EDIT]
Your issue probably comes from the syntax with the dollar sign before the timestamp field which is missing in your code. Try this
{ $gt: ["$timeStamp", 1432201420790] }
instead of
{ timeStamp: { $gt: 1432201420790 }}