If you have a view in a MongoDB database that you’d prefer to be a collection, you’re in the right place.
Below is an example of converting a view to a collection in MongoDB.
Example
This example demonstrates how to convert a view into a collection using the MongoDB Database Tools mongodump
and mongorestore
.
If you don’t have these tools installed, see the MongoDB installation guide.
Find a View
First, let’s take a look at the views and collections in the current database:
show collections
Result:
employees owners pets system.views v_pettypes
In this case, v_pettypes
is actually a view.
We can verify that it’s a view with the following query:
db.getCollectionInfos( { "name": "v_pettypes"} )
Result:
[ { "name" : "v_pettypes", "type" : "view", "options" : { "viewOn" : "pets", "pipeline" : [ { "$group" : { "_id" : "$type" } } ] }, "info" : { "readOnly" : true } } ]
We know it’s a view because the type
field has a value of view
.
Convert the View to a Collection
We can now go ahead and convert that view into a collection. We can do this with the MongoDB Database Tools mongodump
and mongorestore
.
We’ll use mongodump
to export the view, and mongorestore
to restore it. Specifically, we’ll pipe the mongodump
output stream into mongorestore
. That way we can do the whole thing at once.
Here’s the code:
mongodump --archive --db=PetHotel --collection=v_pettypes --viewsAsCollections | mongorestore --archive --nsFrom='PetHotel.v_pettypes' --nsTo='PetHotel.pettypes'
You need to run that code from your system’s command line (e.g. a new Terminal or Command Prompt window). Don’t run it from the mongo
shell.
Running that code converted the v_pettypes
view to a collection called pettypes
in the same database.
Strictly speaking, we didn’t actually convert the view to a collection. We simply used the --viewsAsCollections
argument to dump the view as a collection, then we restored that collection back to the database. Therefore original view still exists.
When you export a view as a collection, mongodump
produces a BSON file containing the documents in the view. If you use mongorestore
to restore the produced BSON file, the view will be restored as a collection.
Without using the --viewsAsCollections
argument, mongodump
exports each view’s metadata. If you include a view’s metadata file in a mongorestore
operation, the view is recreated.
Using --viewsAsCollections
also omits all standard collections.
Check the Results
Let’s take another look at our views and collections.
show collections
Result:
employees owners pets pettypes system.views v_pettypes
So we can see that a new collection called pettypes
exists, and the original view still exists.
We can verify that pettypes
is a collection (and not a view) as follows:
db.getCollectionInfos( { "name": "pettypes"} )
Result:
[ { "name" : "pettypes", "type" : "collection", "options" : { }, "info" : { "readOnly" : false, "uuid" : UUID("d183c7e4-44bc-4656-b272-7ad707f8dc62") }, "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } } ]
We can see that the type
field contains a value of collection
, which means that it’s a collection.
Drop the Original View
At this stage we now have the choice of either deleting the original view or leaving it there.
We can drop it like this:
db.v_pettypes.drop()
Now if we check the collections, we can see that the original view is gone and the new collection remains.
show collections
Result:
employees owners pets pettypes system.views