0

I have the following code

//My search array
var charArray=['a','b','c']

{
    name: 'object1',
    myChar: ['a','v','x']
}

{
    name: 'object2',
    myChar: ['f','h','y']
}

I want to get the object which contains a value in its attribute.

I hope to get the first object using this:

db.getCollection('myObjects').find({
    'myChar':{
        $in:charArray
    }
})

The response is null. In fact I tried using $elemMatch without success

db.getCollection('myObjects').find({
    'myChar':{
        $elemMatch: {$in: charArray}
    }
})
5
  • Can you add the expected response to the post ? Commented Feb 3, 2017 at 19:16
  • { name: 'object1', myChar: ['a','v','x'] } Commented Feb 3, 2017 at 19:17
  • did you run the query again ? It should give you the expected response. Commented Feb 3, 2017 at 19:17
  • Yes, but the response is 0 Commented Feb 3, 2017 at 19:20
  • 1
    Possible duplicate of Find documents whose array field contains at least n elements of a given array Commented Feb 3, 2017 at 19:58

1 Answer 1

-1

I thing you have a typo because I replicate your situation in mongodb instance and your $elemMatch solution works just fine.

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.