In MongoDB, you can use the $pull
operator to remove values from an array.
Use $pull
in conjunction with a method such as update()
to update the specified document with the change.
In MongoDB, you can use the $pull
operator to remove values from an array.
Use $pull
in conjunction with a method such as update()
to update the specified document with the change.
In MongoDB, the $addToSet
operator adds a value to an array unless the value is already present in the array.
It’s similar to the $push
operator, except that $push
adds the value even if the value is already present.
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 readingIf 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.
MongoDB has an administration command called dropIndexes
that drops one or more indexes (except the index on the _id
field) from the specified collection.
In MongoDB, the dropIndexes()
method allows you to drop one or more indexes from a collection.
To drop a single index, pass the name of the index or its definition/specification document. If it’s a text index, you can only specify the index name.
To drop multiple indexes, pass the index names in an array.
To drop all indexes (except for the _id
index), don’t pass any arguments.
There are several ways to drop an index in MongoDB, one of which is to use the dropIndex()
method.
It’s pretty simple to use – just pass the name of the index or its definition/specification document. If it’s a text index, you can only specify the index name.
Continue readingIn MongoDB, the db.collection.unhideIndex()
method unhides a hidden index.
A hidden index is one that’s hidden from the query planner. When you unhide an index, it is no longer hidden from the query planner, and it’s immediately available for use.
Continue readingMongoDB introduced the db.collection.hideIndex()
method in MongoDB 4.4.
This method hides an existing index from the query planner. This allows you to evaluate the potential impact of dropping an index without actually dropping the index.
If hiding it has a negative impact, you can use db.collection.unhideIndex()
to unhide the index. This saves you from having to drop the index, then recreate it.
When using mongoimport
to import files into MongoDB, you have the option of specifying a mode to use. These modes determine what happens if there’s already matching documents in the collection that you’re trying to import into.
By default, mongoimport
uses insert
mode, but there are other modes you can use. The mode you use will depend on what you’re trying to do.
Below is an overview of each mode along with examples.
Continue reading