‘truncate’ method is used to remove all the documents from a collection (it do not remove the indexes associated with the collection).
There are two ways to remove a collection.
a. Using truncate method of collection
b. Using truncate method of db
Using truncate method of collection
Syntax
collection.truncate()
Step 1: Let’s create a collection demodb by executing below statement.
db._create("demodb")
127.0.0.1:8529@demo> db._create("demodb") [ArangoCollection 10954, "demodb" (type document, status loaded)] 127.0.0.1:8529@demo> db._collections() [ [ArangoCollection 10043, "_analyzers" (type document, status loaded)], [ArangoCollection 10058, "_appbundles" (type document, status loaded)], [ArangoCollection 10055, "_apps" (type document, status loaded)], [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], [ArangoCollection 10061, "_frontend" (type document, status loaded)], [ArangoCollection 10040, "_graphs" (type document, status loaded)], [ArangoCollection 10052, "_jobs" (type document, status loaded)], [ArangoCollection 10064, "_modules" (type document, status loaded)], [ArangoCollection 10049, "_queues" (type document, status loaded)], [ArangoCollection 10954, "demodb" (type document, status loaded)] ]
Step 2: Let’s add some content to demodb collection by executing below statements.
col = db.demodb
col.save({"id" : 1, "name" : "Krishna"})
Count number of elements in the collection using ‘count’ method.
col.count()
127.0.0.1:8529@demo> col = db.demodb [ArangoCollection 10954, "demodb" (type document, status loaded)] 127.0.0.1:8529@demo> col.save({"id" : 1, "name" : "Krishna"}) { "_id" : "demodb/11019", "_key" : "11019", "_rev" : "_cRclYT2---" } 127.0.0.1:8529@demo> col.count() 1
Truncate the collection elements and confirm the same using count method.
127.0.0.1:8529@demo> col.truncate() 127.0.0.1:8529@demo> col.count() 0
Using truncate method of db
Signature
db._truncate(collection-identifier)
db._truncate(collection-name)
Truncate a collection using id
Step 1: Create a collection with name test.
127.0.0.1:8529@demo> db._create("test") [ArangoCollection 11517, "test" (type document, status loaded)] 127.0.0.1:8529@demo> db._collections() [ [ArangoCollection 10043, "_analyzers" (type document, status loaded)], [ArangoCollection 10058, "_appbundles" (type document, status loaded)], [ArangoCollection 10055, "_apps" (type document, status loaded)], [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], [ArangoCollection 10061, "_frontend" (type document, status loaded)], [ArangoCollection 10040, "_graphs" (type document, status loaded)], [ArangoCollection 10052, "_jobs" (type document, status loaded)], [ArangoCollection 10064, "_modules" (type document, status loaded)], [ArangoCollection 10049, "_queues" (type document, status loaded)], [ArangoCollection 11517, "test" (type document, status loaded)] ]
Step 2: Add some data to the test collection.
127.0.0.1:8529@demo> colId = db.test [ArangoCollection 11517, "test" (type document, status loaded)] 127.0.0.1:8529@demo> colId.save({"id" : 1, "firstName": "Krishna"}) { "_id" : "test/11588", "_key" : "11588", "_rev" : "_cRczMnm---" } 127.0.0.1:8529@demo> colId.count() 1
Step 3: truncate the collection data using collection identifier.
127.0.0.1:8529@demo> db._truncate(colId) 127.0.0.1:8529@demo> colId.count() 0
Truncate a collection using collection name
Step 1: Add some data to the test collection.
127.0.0.1:8529@demo> colId = db.test [ArangoCollection 11517, "test" (type document, status loaded)] 127.0.0.1:8529@demo> colId.save({"id": 123, "firstName" : "Sailu"}) { "_id" : "test/11683", "_key" : "11683", "_rev" : "_cRc1Yd----" } 127.0.0.1:8529@demo> colId.count() 1
Step 2: Truncate the contents of collection.
127.0.0.1:8529@demo> db._truncate("test") 127.0.0.1:8529@demo> colId.count() 0
No comments:
Post a Comment