2019-11-13

MongoDB: db.collection.isView()

I searched the Internets and failed to find an easy answer to this, so I am hoping the search engines will point to this post to understand how to detect the difference between a collection and a view.

Unfortunately, MongoDB-3.x does not have a built-in method called "db.collection.isView()" much like there is "db.collection.isCapped()". You have to iterate over the collections in the extended format and filter out collections that are views.

You can do this with the following query:


db.getCollectionInfos({
  type: {
    $ne: 'view'
  }
});


This will output something like so:


collections.cursor.firstBatch[0]
{
 "name" : "collection",
 "type" : "collection",
 "options" : {
  "capped" : true,
  "size" : 100000000,
  "storageEngine" : {
   "wiredTiger" : {
    "configString" : "...>configuration<..."
   }
  }
 },
 "info" : {
  "readOnly" : false,
  "uuid" : UUID("00000000-0000-0000-0000-000000000000")
 },
 "idIndex" : {
  "v" : 2,
  "key" : {
   "_id" : 1
  },
  "name" : "_id_",
  "ns" : "db.collection"
 }
}

Hope this his helpful to someone else 😊

No comments:

Post a Comment