What db operations are possible in mongodb triggers? - mongodb

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.

Related

Work around to create collection within transaction in 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

Aggregation across multiple databases in MongoDB or solution to sync collections across databases

We have a requirement in our project to write aggregation queries across multiple mongodb databases. I see that it is not possible with Mongo to query another database in an aggregate query. What would my options be in this case?
We would not mind to create the same collections manually across multiple databases as long as these derived collections sync automatically when the primary collection is updated. Does Mongo have any such solutions to keep collections across databases in sync?
"Almost." You can use the Change Stream feature to listen for changes on the source database but you must write a piece of code to insert/update the material into the target database.

MongoDB : How to exclude a collection from generating oplog

I am working on processing the mongodb oplog and I create a collection in mongodb to add the processed data and I don't want this collection to again generate oplog.
I want all other collection to generate oplog but need to exclude one of the collection. How can I achieve this. Is there any settings to let mongodb know not to generate oplog for a collection.
Any help is appreciated.
Collection in local database are not part of replication. So, If you create a collection in the local database and insert records to that, oplog entries are not created.

How to handle databases or collection being created accidentally in mongoDB? [duplicate]

Is there a way to switch off the ability of mongo to sporadically create dbs and collections as soon as it sees one in a query. I run queries on the mongo console all the time and mistype a db or collection name, causing mongo to just create one. There should be a switch to have mongo only explicitly create dbs and collections. I can't find one on the docs.
To be clear, MongoDB does not auto create collections or databases on queries. For collections, they are auto created when you actually save data to them. You can test this yourself, run a query on a previously unknown collection in a database like this:
use unknowndb
db.unknowncollection.find()
show collections
No collection named "unknowncollection" shows up until you insert or save into it.
Databases are a bit more complex. A simple "use unknowndb" will not auto create the database. However, if after you do that you run something like "show collections" it will create the empty database.
I agree, an option to control this behavior would be great. Happy to vote for it if you open a Jira ticket at mongoDB.
No, implicit creation of collections and DBs is a feature of the console and may not be disabled. You might take a look at the security/authorization/role features of 2.6 and see if anything might help (although there's not something that exactly matches your request as far as I know).
I'd suggest looking through the MongoDB issues/bug/requests database system here to and optionally add the feature request if it doesn't already exist.
For people who are using Mongoose, a new database will get created automatically if your Mongoose Schema contains any form of index. This is because Mongo needs to create a database before it can insert said index.

mongodb duplicate a collection within the same database

I want to clone an existing collection, including data and indexes, to a new collection with another name within the same database, using mongodb JSON interface (not the command-line interface).
I've tried:
cloneCollection - didn't work. is for cloning across databases.
aggregate with an $out operator - that copies just the data but not the indexes.
The aggregate command I've tried:
{"aggregate":"orig_coll", "pipeline":[{"$out":"orig_clone"}]}
There is no way to do this in one JSON query.
So, two solutions here :
Using mongodump/mongorestore as proposed in What's the fastest way to copy a collection within the same database?
Using two queries : one to create the destination table with the index and the aggregate query that you already have. I understand that it's not a perfect solution as you need to maintain the query to create the index on the destination table and the index on the source table but there's no other way to do this.
What you need to understand is that, the JSON interface as you told it is not a database interface but a database JavaScript query language. So you can pass query to it not command. In fact, it's not an interface just a query DSL. The interface is the mongo shell or any of the mongo drivers (java, perl, ...) or any of the mongo admin tools ...