To break down my question:
Lets say I have 5 books, each book has details on the authors(eachbook has multiple authors), and details on when it was published.
Therefore, i need 5 collections, one for each book, and within each collection I would need many documents on the authors, and one document on the details?
What single query would I need to insert data into 4 of these books at the same time?
i’m using mongodb
2
Answers
That would be a very unusual MongoDB configuration. Typically, additional collections are not created in the regular course of database interactions, only documents.
It would be much more performant to have one collection called ‘Books’ and each book would be its own document within that collection.
You could create an author as a sub-document within each book document, but that would be a lot of writes and cumbersome to maintain if there are changes.
In this case, it would be best to apply a more Relational Database type strategy and have a separate collection called ‘Authors’, containing a document with the details of each author. You could then store an array of author IDs on corresponding books and use the $lookup method to join Author data on Book data.
Inserting many documents in one go is no problem. The method depends on what MongoDB driver you are using. In JavaScript/Node.js for example, insertMany() accepts an array of objects.
Insert Multiple Documents
collection. insertMany() can insert multiple documents into a collection. Pass an array of documents to the method. If the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document.
you can insert manny documents like this . _id is optional if you not take it then _id will automatically generated.