New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching for Mongo ObjectIds in fields other than _id #1188

Closed
DMcP89 opened this Issue May 8, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@DMcP89
Copy link

DMcP89 commented May 8, 2015

Hi,

I have a document setup in my MongoDB setup like this:

db.find.match_reports({"_id" : ObjectId("55497d3002a6ce36393c0828")});
{
"_id" : ObjectId("55497d3002a6ce36393c0828"),
"match_id" : ObjectId("55497d3002a6ce36393c0827"),
"Type" : "Hockey",
"Winner_Username" : Null
"Loser_Username" : Null
}

I know that when you call a find based on the _id field you can just pass a string, so for example:

MatchReports::find('55497d3002a6ce36393c0828');

will return the document above. The issue I ran into was with calling a find on the match_id field. I assumed it would work similar to a find on the _id field since they were both ObjectIds, so I tried this:

$matchReport = MatchReports::find('first', array('conditions' => array('match_id' => "55497d3002a6ce36393c0827")));

I found I had to call the find using a MongoId object instead of a plain string, which in hindsight makes sense, but it would be nice to be able to search for ObjectIds based on strings alone. Is this expected behavior and are there any plans to implement a feature like this into the Model::find method?

@nateabele

This comment has been minimized.

Copy link
Member

nateabele commented May 9, 2015

What does your $_schema look like? It should cast string IDs to MongoIds if the field is identified as an id in the schema.

@mariuswilms mariuswilms removed the enhancement label May 9, 2015

@DMcP89

This comment has been minimized.

Copy link
Author

DMcP89 commented May 9, 2015

I don't currently have a $_scheme set for this model. I will give that a try, thanks for the tip.

@DMcP89 DMcP89 closed this May 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment