In MongoDB, you can use the $push
operator to append a value to an array.
You can use various modifiers to specify the value’s position in the array, the order of the elements in the array, append multiple values, etc.
Continue readingIn MongoDB, you can use the $push
operator to append a value to an array.
You can use various modifiers to specify the value’s position in the array, the order of the elements in the array, append multiple values, etc.
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 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.
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.
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.
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 readingWhen 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.
When you create a wildcard index in MongoDB, you have the option of specifying a single field, all fields, or just some.
You can use the wildcardProjection
parameter to include or exclude specific field paths from the wildcard index. This article presents an example of including specific fields in the wildcard index.