Geospatial $near within current document field value

Yes it’s possible. You just use $geoNear instead. Beware the catches and read carefully. Presuming that your intent to is to store a field such as “travelDistance” to indicate on the document that any such searches must be “within” that supplied distance from the queried point to be valid. Then we simply query and evaluate … Read more

How to access overall document count during arithmetic aggregation expression

One way to do it is use $setWindowFields: db.collection.aggregate([ { $setWindowFields: { output: { totalCount: {$count: {}} } } }, { $unwind: “$items” }, { $group: { _id: “$items.defindex”, count: {$sum: 1}, totalCount: {$first: “$totalCount”} } }, { $project: { count: 1, usage: {$divide: [“$count”, “$totalCount”] } } }, {$sort: {count: -1}} ]) As you … Read more

MongoDB: how to parse date in 3.6 mongoDb version?

Use $dateToParts and its counterpart $dateFromParts Here is an updated Playground What it does is basically break the date into its parts: { $project: { dateHour: { $dateToParts: { date: “$updatedAt” } } } } would produce: { “dateHour”: { “day”: 19, “hour”: 18, “millisecond”: 0, “minute”: 21, “month”: 3, “second”: 5, “year”: 2020 } … Read more