Tyler
Tyler

Reputation: 11509

MongoDB not returning all results with find $in

I have a simple query to find multiple documents matching a field jId. Right now there are just 2 documents. Using node-mongodb-native and calling find with an $in flag limits it to only one though:

let collection = db.collection('documents')

// Without $in, both are returned
collection
  .find()
  .toArray((err, docs) => {
    docs.map((doc) => doc.jId)) // Gives ['j-04e347','j-548240']
  })

// With $in, only one is returned
collection
  .find({ 'jId': { '$in': [ 'j-04e347',' j-548240' ] } })
  .toArray((err, docs) => {
    docs.map((doc) => doc.jId)) // Gives ['j-04e347']
  })

Is this a bug, or am I using the $in operator incorrectly, or does the node-mongodb-native package not support this?

If $in is not usable, is there another way to achieve the same effect?

Upvotes: 0

Views: 1072

Answers (1)

Tyler
Tyler

Reputation: 11509

There was an extra space getting included: ' j-548240' instead of 'j-548240'.

Ugh. But this does indeed work in node-mongodb-native.

Upvotes: 2

Related Questions