Query MongoDB with length criteria

后端 未结 4 518
南方客
南方客 2020-12-31 07:40

I have several documents in a MongoDB Collection, with a field \'name\' (which is a String).

How can I perform queries like 7 <= name.length <= 14

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 08:07

    You can use MongoDB's $where query parameter to submit javascript to the server. E.g.,:

    db.myCollection.find( {$where: "(7 <= this.name.length) && (this.name.length <= 14)"} )
    

    http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

提交回复
热议问题