5 Ways to Get the Month from a Date in MongoDB

MongoDB 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 reading

6 Ways to Get the Year from a Date in MongoDB

MongoDB 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 reading

Rename a Field in the Query Results in MongoDB

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 reading

How to Fix “The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION” in SQL Server

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 reading

How to Create an Index with a Specific Name in MongoDB

When you create an index in MongoDB, if you don’t specify a name for it, MongoDB will assign one.

The default name consists of each indexed field name concatenated with the key value for that field. For example, the sort order, or if it’s a text index, the string _text, or if it’s a 2dsphere index, the string _2dsphere, etc.

But you can assign your own name when creating indexes in MongoDB.

Continue reading

Exclude Specific Fields in Wildcard Index in MongoDB

When you create a wildcard index in MongoDB, you have the option of specifying a single field, all fields, or just some.

You also have the option of excluding certain fields. In other words, you can specify all fields except for one or more specific fields.

You can use the wildcardProjection parameter to include or exclude specific field paths from the wildcard index. This article presents an example of excluding specific fields in the wildcard index.

Continue reading