1

This is my Mongoose model of conversation object:

const conversationSchema = mongoose.Schema({
  participants: [
    {
      id_profile: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }
    }
  ]
});

How I can find a conversation object that contains two objects in the array "participants" that have ID = 123456 and ID = 654321? The order of objects in array "participants" can be different.

conversation = {
  participants: [
    { id_profile: '123456' },
    { id_profile: '654321' }
  ]
}

conversation = {
  participants: [
    { id_profile: '654321' },
    { id_profile: '123456' }
  ]
}

How to create a query using Mongoose?

1 Answer 1

2

You can use $all for this query, which finds docs where a field matches all of a set of values:

Conversation.find({'participants.id_profile': {$all: ['123456', '654321']}})
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.