MongoDB 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 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 readingMongoDB provides quite a few aggregation pipeline operators for working with dates. This includes 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. This therefore allows you to use JavaScript to extract date values and date parts, etc from a field.
This article presents 6 ways to return the year portion from a date in MongoDB.
Continue readingIn MongoDB, you can use the $unset
field update operator to completely remove a field from a document.
The $unset
operator is designed specifically to delete a field and its value from the document.
Sometimes when you query a collection in MongoDB, you might not be happy with the field names. By default, the field names are simply a reflection of the field names in the actual documents.
Perhaps the field names are inconsistent, or there’s a typo. Whatever the reason, you can use the $project
aggregation pipeline stage to rename a field in your query results.
In some ways, this is comparable to using an alias in SQL, as it doesn’t rename the underlying fields, it simply renames them in the query results.
Continue readingWhen you use methods such as find()
or findOne()
in MongoDB, by default you get the whole document returned. And if you use projections, you can return specific key/value pairs.
But what if you only want the value?
You can extract the value of a field by appending that field’s name to your query when using findOne()
.
If you’re receiving error Msg 3902, Level 16, which reads “The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION”, it’s probably because you’ve got a stray COMMIT
statement.
You could be getting this due to implementing error handling, and forgetting that you’ve already committed or rolled back the transaction elsewhere in your code.
Continue readingIn MongoDB, when you create an index on a field that holds an array, it’s automatically created as a multikey index.
Multikey indexes support efficient queries against array fields.
Multikey indexes can be created for arrays that hold scalar data (e.g. strings, numbers, etc) and nested documents.
Continue readingMongoDB provides the following geospatial index types that support geospatial queries.
2dsphere
indexes support queries that calculate geometries on an earth-like sphere.In this article, I create a 2dsphere
index.