0

我在用 mongoose 嵌套一些子文档时遇到了一点问题。我阅读了很多关于此的内容并尝试了几乎所有内容。

我真的不知道出了什么问题。我知道网络上有很多关于这个的问题。很多都是相似的,但没有解决我的问题。

问题是,我填充的文档总是空的。

  • 我检查了我的架构
  • 我删除了整个数据库
  • 之后,我再次重写了整个架构和代码。

没有任何帮助。我认为这是一个非常小的问题,但我不明白。

这是我的架构:

探针.js

const ProbeShema = new mongoose.Schema({
        device: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Device'
        },
        value: {
                type: String,
                required: true,
                enum: ['Energy', 'Consumption', 'Voltage', 'Current', 'Frequency', 'Time', 'Date']
        },
        unit: {
                type: String,
                required: true,
                enum: ['kWh', 'kW', 'V', 'A', 'Hz', 'time', 'date']
        },
        oid: {
                type: String,
                required: true
        },
        measurement: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Measurement'
        }
});

const Probe = mongoose.model('Probe', ProbeShema,'probe');
module.exports = Probe;

设备.js

var mongoose = require('mongoose');

const DeviceShema = new mongoose.Schema({
        name: {
                type: String,
                required: true,
                trim: true
        },
        ip: {
                type: String,
                required: true,
                unique: true,
                trim: true
        },
        port: {
                type: String,
                required: true,
                trim: true
        },
        secret: {
                type: String,
                required: true,
                trim: true
        },
        probe: [{
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Probe'
        }]
});

const Device = mongoose.model('Device', DeviceShema,'device');
module.exports = Device;

var express = require('express');
var router = express.Router();
var Device = require('../../models/devices')

Device.find()
   .populate( 'probe')
   .exec()
   .then(devices => {
         console.log(devices)
         res.render('./snmp/snmp', { "title": "SNMP-Devices", "devices": devices });
     })
     .catch(err => {
          console.log(err)
     });

输出:

[{
probe: [],
    _id: 5db94f22d8fb641c18fc05b9,
    name: 'test',
    ip: '1.11.1.1',
    port: '111',
    secret:
     '$2b$10$2umat9QqHSFF9/zVih9LU.UKX4sPh/Lmq5XjSai0/pt42QfLXQb6u',
    __v: 0 
}]

我期待什么:


    _id: 5db94f22d8fb641c18fc05b9,
    name: 'test',
    ip: '1.11.1.1',
    port: '111',
    secret:
     '$2b$10$2umat9QqHSFF9/zVih9LU.UKX4sPh/Lmq5XjSai0/pt42QfLXQb6u',
    probe: [
    {
       device: '5db94f22d8fb641c18fc05b9',
       value: 'Energy',
       unit: 'kWh',
       oid:'1.1.1.1.11',
       measurement: 11
    },{
       device: '5db94f22d8fb641c18fc05b9',
       value: 'Consumption',
       unit: 'kW',
       oid:'1.1.1.1.12',
       measurement: 500
     }
]

MongoDB 文档


{
   "_id":"5db95821d3a65d20200cda64",
   "device":"5db9581fd3a65d20200cda63",
   "oid":"asd",
   "value":"Energy",
   "unit":"kWh","__v":0
}


{
   "_id":"5db9581fd3a65d20200cda63",
   "probe":[],
   "name":"asd",
   "ip":"1.1.1.1",
   "port":"1",
   "secret":"$2b$10$mr4x4ls0J2CqdNxnk/ih2.FAHpj1fsXT2NsgrzDXF9y4LCnZBNfXa",
   "__v":0
}

4

3 回答 3

1

我坚信问题的出现是因为循环参考(设备有参考到探针,探针有参考到设备)

在设备模型中,我会删除设备的引用。

probe.js(为了简单起见,我删除了设备的引用,并没有包含其他字段)

const mongoose = require("mongoose");
const ProbeShema = new mongoose.Schema(
  {
    value: {
      type: String,
      required: true,
      enum: [
        "Energy",
        "Consumption",
        "Voltage",
        "Current",
        "Frequency",
        "Time",
        "Date"
      ]
    }
  }
);


const Probe = mongoose.model("Probe", ProbeShema, "probe");
module.exports = Probe;

device.js(为简单起见,我没有包含其他字段)

var mongoose = require("mongoose");

const DeviceShema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  probe: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Probe"
    }
  ]
});

const Device = mongoose.model("Device", DeviceShema, "device");
module.exports = Device;

通过这些更改,现在您可以使用如下探针获取设备:

router.get("/device", async (req, res) => {
  const devices = await Device.find({}).populate("probe");

  res.send(devices);
});

这将为您提供如下结果集:

[
    {
        "probe": [
            {
                "_id": "5db96a460ef8cc1c9c45e7f4",
                "value": "Energy",
                "__v": 0
            },
            {
                "_id": "5db96aa30ef8cc1c9c45e7f5",
                "value": "Consumption",
                "__v": 0
            }
        ],
        "_id": "5db96f13d8faa752cc9e2cf9",
        "name": "Device 1",
        "__v": 0
    },
    {
        "probe": [
            {
                "_id": "5db96a460ef8cc1c9c45e7f4",
                "value": "Energy",
                "__v": 0
            },
            {
                "_id": "5db96aa30ef8cc1c9c45e7f5",
                "value": "Consumption",
                "__v": 0
            },
            {
                "_id": "5db96f96b59c3139bce431bd",
                "value": "Voltage",
                "__v": 0
            }
        ],
        "_id": "5db96fb1b59c3139bce431be",
        "name": "Device 2",
        "__v": 0
    }
]

于 2019-10-30T11:01:09.453 回答
0

谢谢你的帮助。我真的误会了什么。它不时变得更清晰。我认为代码本身并没有错。

问题是,填充不是按我预期的那样工作。我必须对此进行一些测试和尝试才能了解更多信息。对我来说似乎仍然不清楚。我来自 MySQL,其处理方式完全不同。

关于如何保存不同模型之间的约束等等,我有很多麻烦。我认为填充只是比较 id,然后嵌套它,但事实并非如此。(!?)

它现在似乎可以工作了,因为我在数组 probs 中添加了 id。

我有很多问题,但我必须自己回答。当你认为自己如此接近目标并看到隧道尽头的光时真的很难,但实际上你刚刚到达隧道^^

谢谢大家,很抱歉等待时间和我的英语不好。

此致

于 2019-11-01T12:47:58.273 回答
0

你可以试试这个——

Device.aggregate([
// Unwind the source
{ "$unwind": "$devices" },
// Do the lookup matching
{ "$lookup": {
   "from": "probe",
   "localField": "probe",
   "foreignField": "_id",
   "as": "proeObjects"
}},
// Unwind the result arrays ( likely one or none )
{ "$unwind": "$proeObjects" },
// Group back to arrays
{ "$group": {
    "_id": "$_id",
    "devices": { "$push": "$devices" },
    "proeObjects": { "$push": "$proeObjects" }
}}
], (err, result) => { console.log(err || )})
于 2019-10-30T11:33:36.073 回答