auhuman
auhuman

Reputation: 972

Difference between find and findOne

when a variable say testVar is assigned with result of findOne, its availability is endless. But a variable assigned with result of find() availability is only once. Below is the command prompt dump

> var testVar = db.basic.findOne()
> testVar
{ "_id" : ObjectId("52abd2737164a542e93f1ebe"), "name" : "MongoDB" }
> testVar
{ "_id" : ObjectId("52abd2737164a542e93f1ebe"), "name" : "MongoDB" }
> testVar
{ "_id" : ObjectId("52abd2737164a542e93f1ebe"), "name" : "MongoDB" }


> var testVar = db.basic.find({"name":"MongoDB"})
> testVar
{ "_id" : ObjectId("52abd2737164a542e93f1ebe"), "name" : "MongoDB" }
> testVar
> testVar
>

Upvotes: 2

Views: 5127

Answers (1)

Jeff Storey
Jeff Storey

Reputation: 57192

findOne returns a single document, where find returns a cursor. Once you go through the cursor of find, you are at the end, and there are no more documents.

Upvotes: 14

Related Questions