Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid where vs find when using id as criteria

when using document's id as the only criteria in my query what's the difference between:

Board.only(:_id).find(params[:board_id])

and

Board.where(_id: params[:board_id]).only(:_id)

the only thing i've noticed is that printing the result as json when using where it encloses the result in square brackets

like image 459
Matteo Pagliazzi Avatar asked Mar 23 '12 19:03

Matteo Pagliazzi


2 Answers

find returns one document.

where returns an array of documents that match the criteria.

like image 200
Kyle Avatar answered Nov 18 '22 20:11

Kyle


To add to Kyle's answer:

If matching record is not found: find throws exception, where returns empty enumerable.

like image 32
Sergio Tulentsev Avatar answered Nov 18 '22 21:11

Sergio Tulentsev