猫鼬的“与众不同"功能支持正则表达式查询? [英] Does mongoose's "distinct" function support regex in a query?

查看:62
本文介绍了猫鼬的“与众不同"功能支持正则表达式查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在node/mongoose项目中我有以下js代码.我正在处理自动完成表格.使用常规的查找"就可以了,但是我想做一个明显的"查找.

I have the following bit of js in a node/mongoose project. I'm working on an autocomplete form. It works just fine with a regular "find", but I want to do a "distinct" find instead.

所以这是我到目前为止的位置.我认为问题在于查询的形成方式.有人可以在我的语法中帮助我吗?还是仅仅是猫鼬的"distinct"不支持可选查询中的正则表达式?

So here's where I'm at so far. The problem I believe is in the way the query is formed. Can someone help with my syntax in the distinct line? Or is it just that mongoose's "distinct" doesn't support regex in an optional query?

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);
});

这是我的超级详细(-vvvvvvvvvvvvvvv)猫鼬控制台显示的内容:

And here's what my super-verbose (-vvvvvvvvvvvvv) mongoose console is showing:

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

有什么想法吗?

推荐答案

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

Answering my own question. I did indeed have syntax errors in mongoose's distinct method. It accepts 3 params, I only had 2. The correct syntax with a regex (or any condition) is:

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

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

From Mongoose docs for Model.distinct():

Model.distinct(field, conditions, callback);

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

这篇关于猫鼬的“与众不同"功能支持正则表达式查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆