Work around to create collection within transaction in mongoDB - mongodb

I have REST apis that creates mongoDB collection at runtime and keeps the collection name in another collection.
MongoDB now supports transaction and does allow only CRUD operations within a transaction not create collection operation.
Im thinking that to keep all the collection names within a transaction in request context and create the collections once transaction is completed. Is there any other way or workaround to solve this ?

Starting from Mongo 4.4, you can create collections and indexes inside transactions. Documentation

Related

How to monitor mongo db collection to find which indexes should be added which should be removed?

I have a collection in Azure Mongo Db. Is there any tools which can monitor all request operation with goal to decide which indexes should be added, and which are redundant and should be removed?

Mongo dynamic collection creation and locking

I am working on an app where I am looking into creating MongoDB collections on the fly as they are needed.
The app consumes data from a data source and maps the data to a collection. If the collection does not exist, the app:
creates the collection
kicks off appropriate indexes in the background
shards the collection
and then inserts the data into the collection.
While this is going on, other processes will be reading and writing from the database.
Looking at this MongodDB locking FAQ, it appears that the reads and writes in other collections of the database should not be affected by the dynamic collection creation snd setup i.e. they won't end up waiting on a lock we acquired to create the collection.
Question: Is the above assumption correct?
Thank you in advance!
No, when you insert into a collection which does not exist, then a new collection is created automatically. Of course, this new collection does not have any index (apart from _id) and is not sharded.
So, you must ensure the collection is created before the application starts any inserts.
However, it is no problem to create indexes and enable sharding on a collection which contains already some data.
Note, when you enable sharding on an empty collection or a collection with low amount of data, then all data is written initially to the primary shard. You may use sh.splitAt() to pre-split the upcoming data.

Does creating a new Database instead of just a new Collection provide any performance benefits in MongoDB 4.4?

I'm creating a Collection for storing chat messages. This Collection will be read and written very frequently. Should I create a new Database and create my chat Collection there, or just create my chat Collection in my existing Database?
I've read that WiredTiger only uses document level locks. Does this mean there will be no performance difference between the two mentioned options?

What db operations are possible in mongodb triggers?

I consider using mongo db triggers for some data maintenance.
I will need to:
rename collection
create new collection
copy index from one collection to another
Is it possible using triggers?
I couldn't find it yet in the documentation.

Create MongoDB Dynamic view

I'm new in MongoDB, and i would like to know if there is a way to create a dynamic view inside MongoDB.
Let me be more precise :
In mongo i have a collection with financial datas, and an GUI interface wich display the datas.
But each users could reduce the datas by adding or removing columns to the grid, and filter the grid : a classic usecase.
What i would like to do, is to create a collection for each user that listen to the master table, according to the users filters: something like this :
mongo.createView(masterCollection, filters, mapReduce)
In this scenario, mongo update each view, each time a modification is done in the master collection (update, delete, insert).
I could do something like this manualy : create a table for a user, and use a tailable cursor on my master collection with the user filters and mapReduce, and the add, remove, or update the document in the user collection.
But, i have up to 100 simultaneous users, and it would open and keep alive 100 tailable cursors on the primary collection. I don't know enough mongo, but i think it's not a best practice to do something like this.
Actualy i have a thread for each user that get data for the collection, according to the user filters, every 5 secondes.
Could you please let me now if there is a native mongo way to handle this scenario or a way to create a user view in mongo.
Thank you
Starting with MongoDB v3.4 (community and enterprise edition), there is a support for creating read only views from existing collections or other views. This feature utilises MongoDB aggregation pipeline.
To create a view from the mongo shell, you could do:
db.createView(<view>, <source>, <pipeline>, <collation> )
Using your scenario above, you could create:
db.createView(<"user_view_name">, <"source_collection_name">, <"aggregation pipeline operators">)
See all the available Aggregation Pipeline Operators - i.e. not only you could filter, you could also modify the document structure.
See MongoDB Views behaviour for more information.
MongoDB Enterprise has a feature called 'Single View' which implements database views. It's more for aggregating data from multiple tables (e.g. parent/child relationships), and may be more than what you're looking for but is worth checking out. The downside, it's only available in the pricey Enterprise edition.
Check out the description here