【问题标题】:Does mongoose's "distinct" function support regex in a query?mongoose 的“distinct”函数是否支持查询中的正则表达式?
【发布时间】:2012-01-09 02:26:07
【问题描述】:

我在 node/mongoose 项目中有以下一些 js。我正在处理一个自动完成表单。它适用于常规的“查找”,但我想做一个“不同的”查找。

这就是我目前所处的位置。我认为问题在于查询的形成方式。有人可以在不同的行中帮助我的语法吗?或者仅仅是mongoose的“distinct”不支持可选查询中的正则表达式?

var text.term = 'johnny';
var regex = new RegExp("^"+text.term);
// execute the search
Performance.collection.distinct({lc_actor: regex}, function(err, docs) {
    var names = [];
    for(var nam in docs) {
        // push the lc_actor to the array
    names.push(docs[nam].lc_actor);
    }
    // send back via callback function
    callback(null, names);
});

这是我的超级详细 (-vvvvvvvvvvvvv) mongoose 控制台显示的内容:

Tue Nov 29 13:34:30 [conn1] runQuery called mydb.$cmd { distinct: "performances", query: {}, key: { lc_actor: /^johnny/ } }
Tue Nov 29 13:34:30 [conn1] run command mydb.$cmd { distinct: "performances", query: {}, key: { lc_actor: /^johnny/ } }
Tue Nov 29 13:34:30 [conn1] command mydb.$cmd command: { distinct: "performances", query: {}, key: { lc_actor: /^johnny/ } } ntoreturn:1 reslen:140 526ms

有什么想法吗?

【问题讨论】:

    标签: mongodb node.js distinct mongoose


    【解决方案1】:

    回答我自己的问题。我确实在猫鼬的不同方法中有语法错误。它接受 3 个参数,我只有 2 个。带有正则表达式(或任何条件)的正确语法是:

    Performance.collection.distinct('lc_actor', {lc_actor: regex}, function(err, docs) {
    

    来自 Model.distinct() 的 Mongoose 文档:

    Model.distinct(field, conditions, callback);
    

    http://mongoosejs.com/docs/finding-documents.html

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2011-10-18
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多