mongoose如何在搜索ref _id时直接跳过查找文档

时间:2017-07-24 05:08:14

标签: node.js mongodb express mongoose populate

Schema Lift(models / Lift.js)

var LiftSchema = new mongoose.Schema({
  ID: {
  type : String,
  required: true
  }, 
  Name: {
  type : String,
  required: true
  },
  _OEM: {
  type : Schema.Types.ObjectId,
  ref: 'OEM'
  },
  _Building: {
  type : Schema.Types.ObjectId,
  ref: 'Building'
  },
  LastMaintenanceTime: {
  type: Date, 
  default: Date.now 
  },
  StatusCode : {
  type : Number,
  required: true,
  min: 0,
  max: 3
  },
  Status : {
  type : String,
  required: true,
  enum: ['Normal', 'Warnings', 'Maintenance', 'Error'] 
  }}
  );

=============================================== ======================= Schema OEM(models / OEM.js)

var OEMSchema = new mongoose.Schema({
ID: {
  type : String,
  required: true
 }, 
  Name: {
  type : String,
  required: true
  },
  Address: {
  type : String
  }}
);

路由器/ lifts.js

var Lift = require('../models/Lift.js');
var OEM = require('../models/OEM.js');


router.get('/', function(req, res, next) {
if(req.query.OEMID){

    if(mongoose.Types.ObjectId.isValid(req.query.OEMID) ){
        Lift.
        find({ _OEM: req.query.OEMID }).
        exec(function (err, result) {
        if (err) return next(err);
          res.json(result);
        });
    }
    else{
        res.status(400).send({message: 'Bad Request : invalid OEMID'});
    }


}

我得到的结果是null,如何从OEM获得正确的_id得到完整的结果?

这行代码似乎不起作用。 找到({_OEM:req.query.OEMID})。

使用网址“http://localhost:3000/lifts/OEMID=5971bd7e734d1d6202a81527”, 我期望的结果如下(从10中取回4次升降机)

[
    {
        "_id":"59745b9a734d1d6202a90eb9",
        "ID":"L000001",
        "Name":"Lift AAA",
        "_OEM":{
            "_id":"5971bd7e734d1d6202a81527",
            "ID":"M0001",
            "Name":"Schindler",
            "Address":""
        },
        "_Building":{
            "_id":"5974599d734d1d6202a90e41",
            "ID":"B000001",
            "Name":"Building 1",
            "Address":"3244 Lorem Ipsum dolor Lorem ipsum dolor",
            "Longitude":"12.12",
            "Latitude":"34.34",
            "StatusCode":0,
            "Status":"Healthy"
        },
        "StatusCode":3,
        "Status":"Error",
        "_LiftError":{ },
        "LastMaintenanceTime":"2011-09-16T11:05:17.000Z"
    },
    {
        "_id":"59745bd7734d1d6202a90ec4",
        "ID":"L000002",
        "Name":"Lift BBB",
        "_OEM":{
            "_id":"5971bd7e734d1d6202a81527",
            "ID":"M0001",
            "Name":"Schindler",
            "Address":""
        },
        "_Building":{ },
        "StatusCode":0,
        "Status":"Normal",
        "LastMaintenanceTime":"2011-09-16T11:05:17.000Z"
    },
    {
        "_id":"59745da8734d1d6202a90f40",
        "ID":"L000007",
        "Name":"Lift GGG",
        "_OEM":{
            "_id":"5971bd7e734d1d6202a81527",
            "ID":"M0001",
            "Name":"Schindler",
            "Address":""
        },
        "_Building":{ },
        "StatusCode":2,
        "Status":"Maintenance",
        "LastMaintenanceTime":"2011-09-16T11:05:17.000Z"
    },
    {
        "_id":"59745e3f734d1d6202a90f59",
        "ID":"L000010",
        "Name":"Lift JJJ",
        "_OEM":{
            "_id":"5971bd7e734d1d6202a81527",
            "ID":"M0001",
            "Name":"Schindler",
            "Address":""
        },
        "_Building":{ },
        "StatusCode":0,
        "Status":"Normal",
        "LastMaintenanceTime":"2011-09-16T11:05:17.000Z"
    }
]

但我现在就明白了

[]

顺便说一下,我在Mongo上投下的OEM文件如下 enter image description here

和电梯文件 enter image description here

0 个答案:

没有答案