w3resource
MongoDB Tutorial

MongoDB: db.collection.validate() method

db.collection.validate

The db.collection.validate() method is used to validate a collection. The method scans a collections data structures for correctness and returns a single document that describes the relationship between the logical collection and the physical representation of the data.

Syntax:

db.collection.validate(full)

Parameter:

Name Description Required /
Optional
Type
full Specify true to enable a full validation and to return full statistics. MongoDB disables full validation by default because it is a potentially resource-intensive operation. Optional boolean

Sample document in the restaurants collection:


{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}
.....

Example: MongoDB: db.collection.validate() method

The following statement scans the data structures of collection restaurants for correctness and returns the single document.

db.restaurants.validate();

Output:

> db.restaurants.validate();
{
        "ns" : "test.restaurants",
        "firstExtent" : "0:28000 ns:test.restaurants",
        "lastExtent" : "0:b72000 ns:test.restaurants",
        "extentCount" : 7,
        "datasize" : 14721552,
        "nrecords" : 25359,
        "lastExtentSize" : 11325440,
        "padding" : 1,
        "firstExtentDetails" : {
                "loc" : "0:28000",
                "xnext" : "0:4a000",
                "xprev" : "null",
                "nsdiag" : "test.restaurants",
                "size" : 8192,
                "firstRecord" : "0:280b0",
                "lastRecord" : "0:29eb0"
        },
        "lastExtentDetails" : {
                "loc" : "0:b72000",
                "xnext" : "null",
                "xprev" : "0:372000",
                "nsdiag" : "test.restaurants",
                "size" : 11325440,
                "firstRecord" : "0:b720b0",
                "lastRecord" : "0:f358b0"
        },
        "deletedCount" : 7,
        "deletedSize" : 7378992,
        "nIndexes" : 1,
        "keysPerIndex" : {
                "test.restaurants.$_id_" : 25359
        },
        "valid" : true,
        "errors" : [ ],
        "warning" : "Some checks omitted for speed. use {full:true} option to do more thorough scan.",
        "ok" : 1
}

Retrieve the restaurants data from here

Previous: db.collection.update() method
Next: cursor.batchSize() method