0

I'm using Angular 7 with node.js, express and mongoose

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.

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?

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") }
 ] }

here is my Schema:

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

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;
Maxim
  • 1
  • 2
  • can't you use subdocuments etc? – jcuypers Mar 06 '19 at 09:47
  • 1
    I'm working with these technologies for a month now... I don't know all best practices yet :P ...didn't know about that yet... I'll try it out – Maxim Mar 06 '19 at 09:56
  • don't worry, just giving idea's. – jcuypers Mar 06 '19 at 09:57
  • `Diagram.updateOne( { KdNr: req.params.KdNr }, { $push: { domains: { $each: data.domains } } })`. Not clear if you really mean `upsert`, and the `multi` is not a valid option anyway, so I just figured you "cargo culted" the code. More explanation on the [linked answer](https://stackoverflow.com/questions/11963684/how-to-push-an-array-of-objects-into-an-array-in-mongoose-with-one-call) – Neil Lunn Mar 06 '19 at 10:22
  • Indeed, it is useless at this point... i just tried something earlier and forgot to take it out... problem is still remaining... i still get onjectId() instead of the data... maybe its something else i dont understand yet – Maxim Mar 06 '19 at 10:30
  • Here some references that helped me before: https://stackoverflow.com/questions/16514912/mongoose-schema-for-hierarchical-data-like-a-folder-subfolder-file, I will additionally post an answer on which I use such a type of behaviour. – jcuypers Mar 06 '19 at 10:49
  • @NeilLunn Why duplicate? Im not trying to add multiple Objects into an array... i want to know why I'm saving the ObjectId() with no other values instead my nested array, like shown in examples – Maxim Mar 06 '19 at 13:02

1 Answers1

0

Try this in

Diagram.updateOne

{ $push:{"domains": data }}

  • I tried... didn't change the behaviour... it still saves the ObjectId() instead of the data – Maxim Mar 06 '19 at 10:17