2

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

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.