Creating a new collection and drop()

When Shannon created a new collection, he used this code:

db = db.getSiblingDB("video");
db.movieDetails.drop();
db.movieDetails.insertMany([,,,]);

Should we always use drop() before creating a new collection and why exactly Shannon used this method?

Also I don’t quite understand how getSiblingDB() works and why is it here. Is it possible to create a collection using just db.createCollection() method?

Please help to clear things up))

No
drop() option is not needed before insert if collection does not exist
In this case collection exists.So he wanted to cleanup old one and import fresh data

From mongo docs on db.getSiblingDB
You can use db.getSiblingDB() as an alternative to the use helper. This is particularly useful when writing scripts using the mongo shell where the use helper is not available. Consider the following sequence of operations:

db = db.getSiblingDB(‘users’)
db.active.count()
This operation sets the db object to point to the database named users, and then returns a count of the collection named active. You can create multiple db objects, that refer to different databases, as in the following sequence of operations:

users = db.getSiblingDB(‘users’)
records = db.getSiblingDB(‘records’)

ex:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db
100YWeatherSmall
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db = db.getSiblingDB(‘video’)
video
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db
video

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db
video
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.getSiblingDB(‘city’)
city
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db
video

2 Likes

Thank you very much!