Monday, 9 August 2021

ArangoDB: firstExample: Get one document that matches to given example

‘collection.firstExample(example)’ method return one document that matches to given example. If not document matches to given example, it return null.

127.0.0.1:8529@abc_org> db.user.all().toArray()
[ 
  { 
    "_key" : "15950", 
    "_id" : "user/15950", 
    "_rev" : "_cReaqJC---", 
    "id" : 1, 
    "firstname" : "Sailu", 
    "lastName" : "Ptr", 
    "age" : 32 
  }, 
  { 
    "_key" : "15970", 
    "_id" : "user/15970", 
    "_rev" : "_cRebGD2---", 
    "id" : 2, 
    "firstname" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 33 
  }, 
  { 
    "_key" : "15978", 
    "_id" : "user/15978", 
    "_rev" : "_cRebX7q---", 
    "id" : 3, 
    "firstname" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 32 
  }, 
  { 
    "_key" : "15998", 
    "_id" : "user/15998", 
    "_rev" : "_cRebx9K---", 
    "id" : 4, 
    "firstname" : "Venkat", 
    "lastName" : "Ptr", 
    "age" : 35 
  } 
]

Let’s get one user document whose age is 32.

127.0.0.1:8529@abc_org> db.user.firstExample({"age": 32})
{ 
  "_key" : "15950", 
  "_id" : "user/15950", 
  "_rev" : "_cReaqJC---", 
  "id" : 1, 
  "firstname" : "Sailu", 
  "lastName" : "Ptr", 
  "age" : 32 
}


Let’s get one user document whose age is 40.

127.0.0.1:8529@abc_org> db.user.firstExample({"age": 40})
null


Alternative syntax

collection.firstExample(path1, value1, ...)

127.0.0.1:8529@abc_org> db.user.firstExample("age", 32, "firstname", "Sailu")
{ 
  "_key" : "15950", 
  "_id" : "user/15950", 
  "_rev" : "_cReaqJC---", 
  "id" : 1, 
  "firstname" : "Sailu", 
  "lastName" : "Ptr", 
  "age" : 32 
}


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment