‘collection.exists(object)’ method determines whether a document exists in the collection or not.
Signature
collection.exists(object_id)
collection.exists(object_key)
Let’s experiment with an example.
127.0.0.1:8529@abc_org> db.user.toArray() [ { "_key" : "17089", "_id" : "user/17089", "_rev" : "_cRe1GKq---", "id" : 1, "firstName" : "Sailu", "lastName" : "Ptr", "age" : 32 }, { "_key" : "17091", "_id" : "user/17091", "_rev" : "_cRe1GKu---", "id" : 2, "firstName" : "Gopi", "lastName" : "Battu", "age" : 33 }, { "_key" : "17093", "_id" : "user/17093", "_rev" : "_cRe1GKy---", "id" : 3, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 32 }, { "_key" : "17095", "_id" : "user/17095", "_rev" : "_cRe1H-6---", "id" : 4, "firstName" : "Venkat", "lastName" : "Ptr", "age" : 35 } ]
Check for the existence by _id
127.0.0.1:8529@abc_org> db.user.exists("user/17095") { "_id" : "user/17095", "_key" : "17095", "_rev" : "_cRe1H-6---" }
Check for the existence by _key
127.0.0.1:8529@abc_org> db.user.exists("17095") { "_id" : "user/17095", "_key" : "17095", "_rev" : "_cRe1H-6---" }
You can even pass a json object as an argument to exists method.
db.user.exists({"_key" : "17095"})
db.user.exists({"_id" : "user/17095"})
127.0.0.1:8529@abc_org> db.user.exists({"_key" : "17095"}) { "_id" : "user/17095", "_key" : "17095", "_rev" : "_cRe1H-6---" } 127.0.0.1:8529@abc_org> db.user.exists({"_id" : "user/17095"}) { "_id" : "user/17095", "_key" : "17095", "_rev" : "_cRe1H-6---" }
This method throws a conflict when _rev is specified but the document found has a different revision already.
Document with correct revision
127.0.0.1:8529@abc_org> db.user.exists({"_id" : "user/17095", "_rev" : "_cRe1H-6---"}) { "_id" : "user/17095", "_key" : "17095", "_rev" : "_cRe1H-6---" }
Document with other revision
127.0.0.1:8529@abc_org> db.user.exists({"_id" : "user/17095", "_rev" : "_cRe1H-6"}) JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1200: conflict ! throw error; ! ^ stacktrace: ArangoError: conflict at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21) at ArangoCollection.exists (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:811:12) at <shell command>:1:9
No comments:
Post a Comment