1

Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks

Categories.find({'_id': 'parentID'})

{
   _id: 11,
   parentID: 1
}

I am using MongoDB 2.6.7

MChan
  • 6,842
  • 27
  • 83
  • 132

1 Answers1

1

Yes, this is easy to do with the $where operator.

db.Categories.find({'$where':"this._id === this.parent"})

This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.

SolarBear
  • 4,534
  • 4
  • 37
  • 53