我在用猫鼬嵌套一些子文档时遇到了一些问题。我读了很多,并尝试了几乎所有内容。
我真的不知道怎么了。 我知道网络上确实有很多问题,很多都是相似的,但没有任何解决方案可以解决我的问题。
问题是,我填充的文档始终为空。
没有任何帮助。 我认为这是一个很小的问题,但我不明白。
probe.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;
devices.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
}
]
{
"_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
}
答案 0 :(得分: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
}
]
答案 1 :(得分: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 || )})
答案 2 :(得分:0)
感谢您的帮助。 我真的误会了。它会不时变得更加清晰。 我认为代码本身并没有错。
问题是,无法正常工作。 我必须测试一下,才能更多地了解这一点。 我似乎还不清楚。 我来自MySQL,其处理方式完全不同。
我在如何保存不同模型之间的约束等方面遇到很多麻烦。我认为填充只是比较id,然后将其嵌套,但事实并非如此。 (!?)
它现在似乎可以正常工作,因为我在数组概率中添加了ID。
我有很多问题,但我必须自己回答。 当您以为自己离目标很近并且看到隧道尽头的光线时,这真的很难,但是实际上您刚刚到达隧道^^
谢谢大家,对您的等待时间和我的英语不好对不起。
最诚挚的问候