使用$ where的内部字段比较查询

时间:2012-10-16 13:29:13

标签: mongodb

我有一个名为people的集合。

我知道我可以这样做:

db.people.find( { $where: "this.lastname == this.firstname" } );

但我怎么能这样做:

db.people.find( { $where: "this.lastname == Acquaintances.lastname" } );

人类翻译:所有至少有一个熟人同名的人。

Acquaintances是一个子文档数组。

1 个答案:

答案 0 :(得分:0)

mongodb $where命令类似于map reduce,它处理提取数据上的javascript表达式。它只适用于慢速运行的后台作业。在任何正常情况下都不要使用它。

function () {
    var arr = [];
    for (i in this.Acquaintances) {
        arr.push(this.Acquaintances[i].lastname);
    }
    return arr.indexOf(this.lastname) > -1;
}

db.people.find(f)

检查此link以获取更多信息abt $where