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

$unionWith – MongoDB’s Equivalent of UNION ALL

If you’re familiar with SQL, you might know about the UNION clause, which concatenates the results of two queries into a single result set. In particular, UNION ALL includes duplicates.

In MongoDB, we can use the $unionWith aggregation pipeline stage to achieve the same effect that UNION ALL produces. The $unionWith stage performs a union of two collections – it combines pipeline results from two collections into a single result set. And it includes duplicates.

Continue reading

List of Date Formats Available with CONVERT() in SQL Server

The following table contains a list of the date formats that you can provide to the CONVERT() function when you convert a date/time value to a string.

These formats are provided as an optional third argument when calling the CONVERT() function. They’re provided as an integer expression that specifies how the CONVERT() function will format the date. 

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