You.Brighton You.Brighton - 2 months ago 8
Node.js Question

Mapping other collection by same schema name on Mongoose

Engine Collection

var EngineSchema = new mongoose.Schema({

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

});


Oil Collection

var OilSchema = new mongoose.Schema({

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

});


I made simple

Engine DB

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

OilDB

[ { _id: 57d9614265e6c402a2c09990, name: 'EDGE', ll04: '1', __v: 0 },

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

If I have the N54 B30A than It needs classification about ll98, ll01, ll04

EDGE Product only one about classification ll04

totally I choose the 'N54 B30A' that has ll04 classification so I want to appear something that has the ll04 classification

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

ll04 mapping?

Select N54 B30A -> appear EDGE

My Code Simple but It did't execute

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


More Question
I want to search many kind atrribute(ll01, ll01fe, ll04) such as

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


but I don't know how to add one item like {ll01 : 1} than add {ll01le:1} and {ll04:1}
I tried coding

My Code

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


I'm not useful javascript so I made by only my thinking. is this right?

Answer

You cannot assign a value to JSON as {result[2] : 1}. Change your code to this

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;
    Oil.findOne(oilFindQuery, function (err, oil) {
        console.log(oil)
    });
})