【问题标题】:Mongoose find on field combination猫鼬在字段组合上找到
【发布时间】:2016-05-23 19:41:00
【问题描述】:

是否可以使用 mongoose 搜索两个字段的串联。

来自 Mysql 的类似这样的东西:

select * from CARS where concat(make, model) = "<car name>";

说,我的汽车架构如下:

var carSchema = new mongoose.Schema({
    model: String,
    make: String,
});

var Car =  mongoose.model('Car', carSchema);

现在有以下可能:

Car.find( {make + model : <car name>}, function(err, car) {

});

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    您需要使用aggregate() 方法来执行此操作。

    Car.aggregate(
        [
            { "$redact": { 
                "$cond": [ 
                    { "$eq": [ 
                        { "$concat": [ "$make", " ", "$model" ] },
                        "<car name>"
                    ]},
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }}
        ],
        function(err, car) {
            // Do something
        }
    )
    

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 2022-01-26
      • 1970-01-01
      • 2020-03-13
      • 2014-08-16
      • 2019-12-02
      • 2019-03-28
      • 1970-01-01
      • 2015-05-02
      相关资源
      最近更新 更多