0

i have a document that store array of strings , i want search by id and by array contain. i used 2 method , both of them return null

the Schema example:

const exampleSchema = new mongoose.Schema{
id : objectId(),
list :Array // will store other id's
}
const Example= mongoose.model("Example", exampleSchema );

// this work
const friend = await Example.findOne({ _id: req.body.friendId }); // return the docuemnt

//this not work - return null
const friend = await Example.findOne({ _id: req.body.friendId, pending: { $in: [req.body.userId] } });



also i tried $elemMatch that not work

1 Answer 1

1

As per the schema you mentioned it has 2 fields, _id and list. So your query need to modify as below.

const friend = await Example.findOne({ _id: req.body.friendId, list: { $in: [req.body.userId] } });
Sign up to request clarification or add additional context in comments.

1 Comment

now i try to understand how to hapdate that list. i post the question here , see if u can help please stackoverflow.com/questions/72957889/…

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.