0

so i'm trying to match if the orders.id match by UserModel.find() then it should return that array.

https://i.sstatic.net/bNjgW.png

For example, I have two elements inside orders array, i need to check which one is equal to that id and then return that element that's equal to this. So it should return orders of index 0 and it's elements

I tried this:

userModel.find({ orders.id: orderID });

but it doesn't return the element

1 Answer 1

1

First, I would add quotes to the field "order.id":

userModel.find({ "orders.id": orderID });

Second, I also see several spaces on the "ObjectId"

object ids with string at the end

So this object id seems to be stored wrong as well. Check the reason that stored those spaces at the end of the object id.

Just in case you may test this query on the Mongo Compass or any other client would like.

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

2 Comments

It doesn't return that elements. It returns the whole user
You have to use the find method's projection parameter to return the only required fields.

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.