Reputation: 734
I have a list of ObjectId's mapped using Morphia. In java, the mapping looks like this:
public class Log {
@Indexed
public List<ObjectId> companyIds;
....
}
In Mongo shell, however, when I search using $elemMatch it will complain about invalid type.
> db.Log.find({ "companyIds" : { "$elemMatch" : ObjectId("5059e90d0364d02be740417a")}})
error: {
"$err" : "invalid parameter: expected an object ($elemMatch)",
"code" : 10065
}
Is there anything else I need to do to use $elemMatch with ObjectId's?
Thanks
Upvotes: 2
Views: 6833
Reputation: 6198
You don't need to use $elemMatch
to do this. You can just run the query
{ "companyIds" : ObjectId("...") }
which will find all objects whose companyIds field contains the given objectId.
According to the docs, $elemMatch
is only necessary when you're trying to match against multiple fields on an array element. The reason it's saying "expected an object" is that $elemMatch
takes a full fledged mongo query (as in, something that you could pass to find
) as it's argument.
Upvotes: 10