0

I'm trying to add values to an existing document in my collection with nested arrays.

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??

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 = {
      domain: req.body.domain,
      in: [{
        content : "New Graph",
        graph : {}
      }],
      out: [{
        content : "New Graph",
        graph : {}
      }]
  }

  Diagram.updateOne(
    { KdNr: req.params.KdNr },
    { $push: { domains: { $each: data } } } // Seems like here is some problem?
    // { $push: data }
  ).then(result => {
    if(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

1 Answers1

0

// $each in $push is only allow to a array of value.so you should be change your data object to array of object

const data =[ {
      domain: req.body.domain,
      in: [{
        content : "New Graph",
        graph : {}
      }],
      out: [{
        content : "New Graph",
        graph : {}
      }]
  }]
Krrish
  • 172
  • 9
  • i tried your solution and got this: UnhandledPromiseRejectionWarning: MongoError: The field 'domains' must be an array but is of type object in document – Maxim Mar 06 '19 at 15:06
  • { $push: { domains: { $each: [data] } } } use like this – Krrish Mar 06 '19 at 15:08
  • same error is still remaining :-( i also tried :{ $addToSet: data }, or { $push: {"domains":data} }, – Maxim Mar 06 '19 at 15:09
  • domain:[ { name: { type: String, unique: true, required: true }, in: { content: { type: String }, graphSchema }, out: { content: { type: String }, graphSchema } }] change your schema like this – Krrish Mar 06 '19 at 15:11
  • mhm... didnt work... also i need domains to be an array not domain... one customer has an array of 'domain' saved in 'domains' array – Maxim Mar 06 '19 at 15:17