0

I want to have a limit on each field itself when using collection.find(). In below example I can place a limit as a method for find, but how to perform it on each field accordingly. Is this available in mongodb?

        var collection = db.collection('collAutocomplete');
        collection.find({
            $or: [{
                "Town": new RegExp(searchKey, 'i')  //Can I put a limit here?
            }, {
                "State": new RegExp(searchKey, 'i')  //Can I put a limit here?
            }, {
                "Nation": new RegExp(searchKey, 'i')  //Can I put a limit here?
            }]
        }).limit(3)   //I can put a limit here..
        .toArray()function(err, item) { }});
FRizal
  • 448
  • 7
  • 15

1 Answers1

0

You can't have limits on conditions as you described. You basically want top n on multiple conditions, so you can solve the problem in different ways, but I doubt there is a "single query" solution for this.

You could use multiple queries or use the aggregation framework to build arrays for your regex-s then get your result from there.

You should take a look at Multiple limit condition in mongodb as well.

Community
  • 1
  • 1
Pio
  • 4,044
  • 11
  • 46
  • 81