If you need to return the size of a document in MongoDB, you can use the following:
- The
$bsonSize
aggregation pipeline operator - The
Object.bsonSize()
method
Below are examples of each approach.
Continue readingIf you need to return the size of a document in MongoDB, you can use the following:
$bsonSize
aggregation pipeline operatorObject.bsonSize()
methodBelow are examples of each approach.
Continue readingIn MongoDB, the cursor.sort()
method specifies the order in which the query returns matching documents.
The sort()
method accepts a document that specifies the field to sort, and the sort order. The sort order can be either 1
for ascending or -1
for descending.
You can also specify { $meta: "textScore" }
when doing $text
searches, in order to sort by the computed textScore
metadata in descending order.
MongoDB offers various methods for counting the documents in a collection or view. There are also some aggregation operators that enable you to count incoming documents from a previous aggregation pipeline stage.
Continue readingMongoDB provides a good range of aggregation pipeline operators for working with dates, including operators that extract certain parts of dates, such as the year, month, day, hours, minutes, etc.
There are also a couple of MongoDB methods that enable you to iterate through a cursor, and apply a JavaScript function. You can therefore use JavaScript to extract date values and date parts, etc from a field as required.
This article presents 5 ways to return the hour portion from a Date object in MongoDB.
Continue readingMongoDB provides various options for extracting date parts from a date.
This article presents 3 ways to return the week portion from a date in MongoDB.
Continue readingWhen extracting the day from a date, the exact option we use will depend on how we want the day to be represented.
For example, do we want the day of the week, the day of the month, or the day of the year? Or perhaps we want it in ISO 8601 format? The return value will usually be different depending on which one we choose.
This article explores those options, and therefore presents 8 ways to return the day portion from a date in MongoDB.
Continue readingMongoDB provides a number of aggregation pipeline operators for working with dates, including operators that extract certain parts of dates, such as the year, month, day, etc.
There are also a couple of MongoDB methods that enable you to iterate through a cursor, and apply a JavaScript function. You can therefore use JavaScript to extract date values and date parts, etc from a field as required.
This article presents 5 ways to return the month portion from a date in MongoDB.
Continue readingThis article contains a list of date operators, methods, and variables that you can use when working with dates in MongoDB.
Continue readingIn MongoDB, the cursor.forEach()
method iterates the cursor to apply a JavaScript function
to each document from the cursor.
In MongoDB, the cursor.map()
method applies a function to each document visited by the cursor and combines the return values in an array.