在Mongoose

时间:2016-09-14 17:55:21

标签: node.js mongodb collections mongoose

引擎收藏

var EngineSchema = new mongoose.Schema({

    name: String,
    GF2: String,
    GF3: String,
    GF4: String,
    ll98: String,
    ll01: String,
    ll04: String

});

油品收集

var OilSchema = new mongoose.Schema({

    name: String,
    GF2: String,
    GF3: String,
    GF4: String,
    ll98: String,
    ll01: String,
    ll04: String

});

我做得很简单

引擎数据库

{_id:57d9604b7ecbc0029a3aad3c,   名称:' N54 B30A',   ll01:' 1',   ll01fe:' 1',   ll04:' 1',   __v:0}

OilDB

[{_id:57d9614265e6c402a2c09990,姓名:' EDGE',ll04:' 1',__ v:0},

{_id:57d98e12acc05505cfd15aae,     名称:' SYNTEC',     GF2:' 1',     GF3:' 1',     GF4:' 1',     __v:0}]

如果我有N54 B30A而不是需要分类ll98,ll01,ll04

EDGE产品只有一个关于分类ll04

我完全选择了N54 B30A'有ll04分类,所以我想出现具有ll04分类的东西

' N54 B30A' - > ll04 ' EDGE' - > ll04

ll04映射?

选择N54 B30A - >出现EDGE

我的代码简单但它没有执行

Engine.findOne({name: 'N54 B30A'}, {_id: 0, name: 0, __v: 0}, function(err, engine) {
    var result = Object.keys(engine.toObject());//[ 'll01', 'll01fe', 'll04' ]
    Oil.findOne({result[2] : 1},function (err, oil) {
        console.log(oil)
    });
})

更多问题 我想搜索许多类型的atrribute(ll01,ll01fe,ll04),例如

findOne({$or:{ll01:1},{ll01le:1},{ll04:1}},function...)

但我不知道如何添加{ll01:1}之类的项目,而不是添加{ll01le:1}和{ll04:1} 我试过编码

我的代码

Engine.findOne({name: 'N54 B30A'}, {_id: 0, name: 0, __v: 0}, function (err, engine) {
    var result = Object.keys(engine.toObject());//[ 'll01', 'll01fe', 'll04' ]
    var Json = [];

    result.forEach(function (item) {
    var code = {};
    code[item] =1;
        Json.push(code);

    });
    console.log(Json);


    Oil.find({$or: Json}, function (err, oil) {
        console.log(oil)
    });
})

我没有用javascript,所以我只想到了我的想法。这是对的吗?

1 个答案:

答案 0 :(得分:0)

您无法将值作为{result[2] : 1}分配给JSON。将您的代码更改为此

Engine.findOne({name: 'N54 B30A'}, {_id: 0, name: 0, __v: 0}, function(err, engine) {
    var result = Object.keys(engine.toObject());//[ 'll01', 'll01fe', 'll04' ]
    var oilFindQuery = {};
    oilFindQuery[result[2]] = 1;

    //or query 
    var orQuery = [];
    result.forEach(function (item) {
        var queryItem = {};
        queryItem[item] = 1
        orQuery.push(queryItem);
    });

    //oilFindQuery = {$or: orQuery};

    Oil.findOne(oilFindQuery, function (err, oil) {
        console.log(oil)
    });
})