How to compare mongo ids?

Today, I was trying to compare two mongoids. I thought it would be really straightforward but it took me around 30-40 minutes to figure out how to compare two mongoids. You need to retrieve the “id” param of MongoId objects you wish to compare. Let’s assume user1 and user2 are two instances of MongoID class and you wish to compare the IDs of these two objects. This is how you do it.

if ($user1->{“$id”} == $user2->{“$id”} ) { …

do blah

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s