为什么要保存ObjectId('key')而不是mongodb中的值? [英] Why do I save the ObjectId('key'), instead of the values in my mongodb?
问题描述
我正在将Angular 7与node.js,express和猫鼬一起使用
I'm using Angular 7 with node.js, express and mongoose
通过猫鼬将对象添加到mongodb时遇到问题,它仅保存"_id":ObjectId('randomID')而不是我想要的值.
I have a problem when adding objects to my mongodb via mongoose, it saves only "_id": ObjectId('randomID') instead of the values i want.
我正在尝试使用嵌套数组将值添加到我的集合中的现有文档中.
I'm trying to add values to an existing document in my collection with nested arrays.
更准确地说:有些客户具有客户编号,客户名称和域. 域应是具有域的数组. 域具有名称值和另外两个数组(称为"in"和"out") 这两个数组都包含带有节点和链接的图数组. 节点具有3个值(id,标签,描述),链接也具有3个值(源,目标,标签)
To be more precise: there are customers with a customer number, customer name and domains. Domains shall be an array with a domain. A domain has a name value and two more arrays (called "in" & "out") both arrays contain a graph array with nodes and links. nodes have 3 values (id, label, description) and links have also 3 values (source, target, label)
我遇到的第一个问题是,我无法将新域添加到域数组中
First Problem I have, is that I cant add new domain to my domains array
我不明白我在做什么错??
I don't understand what am I am doing wrong??
或者在嵌套数组中插入和/或更新嵌套数组的最佳实践是什么?
Or what is best practice to insert and/or update nested arrays inside nested arrays?
我试图理解,为什么我只保存不带任何值的ObjectId(),所以链接的问题对我不起作用.
edit: I try to understand, why am I saving only the ObjectId() with no values, linked question doesn't work for me.
这是我想要在数据库中显示的示例:
here is an Example of what i want in my db:
{
"_id" : ObjectId("5c76a093aac6fa3f140a5672"),
"KdNr" : "10004",
"Kundenname" : "Customer GmbH",
"__v" : 0,
"domains" : [ {
"domain" : "testB.de",
"in" : [ {
"content" : "New Graph B",
"graph" : { ... } } ],
"out" : [ {
"content" : "Another new Graph B",
"graph" : { ... } } ]
}, [ {
"domain" : "testA.de",
"in" : [ {
"content" : "New Graph A",
"graph" : { ... } } ],
"out" : [ {
"content" : "Another new Graph A",
"graph" : { ... } } ]
} ]
}
这是我得到的(不是我想要的)示例:
here is an Example of what i get (not what I want):
{
"_id" : ObjectId("5c76a093aac6fa3f140a5672"),
"KdNr" : "10004",
"Kundenname" : "Customer GmbH",
"__v" : 0,
"domains" : [ {
{ "_id" : ObjectId("5c7f86ad42d63141fc921d04") },
{ "_id" : ObjectId("5c655c828be0b2b295aa126f") }
] }
这是我的架构:
const mongoose = require('mongoose');
const graphSchema = mongoose.Schema({
graph: {
nodes: [{
id: { type: String, required: true, unique: true },
label: { type: String, required: true },
description: { type: String }
}],
links: [{
source: { type: String, required: true },
target: { type: String, required: true },
label: { type: String }
}]
}
});
const domainSchema = mongoose.Schema({
domain: {
name: { type: String, unique: true, required: true },
in: {
content: { type: String },
graphSchema
},
out: {
content: { type: String },
graphSchema
}
}
});
const diagramSchema = mongoose.Schema({
KdNr: { type: String, required: true, index: true, unique: true },
Kundenname: { type: String, required: true },
domains: [{
domainSchema
}]
});
module.exports = mongoose.model('Diagram', diagramSchema);
这是我的domains-routes.js:
here is my domains-routes.js:
// core Modules
const express = require('express');
// own modules
const Diagram = require('../models/diagrams');
const router = express.Router();
// add Domain to Diagram
router.put('/:KdNr', (req, res, next) => {
console.log(req.body.domain);
const data = {
domains : [{
domain: req.body.domain,
in: [{
content : "New Graph",
graph : {}
}],
out: [{
content : "New Graph",
graph : {}
}]
}]
}
Diagram.updateOne(
{ KdNr: req.params.KdNr },
{ $push: data }
).then(result => {
if(result) {
console.log('Diagram found in database:');
console.log(result);
res.status(200).json({ message: 'Diagram saved' });
} else {
res.status(404).json({ message: 'Diagram for customer: '
+ req.params.KdNr + 'not found!'})
}
})
})
module.exports = router;
推荐答案
在
Diagram.updateOne
{ $push:{"domains": data }}
这篇关于为什么要保存ObjectId('key')而不是mongodb中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!