0

I have a nested item schema in an invoice schema as given below :

var InvoiceSchema = new db.Schema({
  title: {
    type: String,
    required: true
  },
  description: String,
  clientId: db.Schema.ObjectId,
  companyId: db.Schema.ObjectId,
  poNumber: Number,
  invoiceNumber: {
    type: Number,
    default: 1000
  },
  taxApplied: {
    type: Boolean,
    default: false
  },
  feeApplied: {
    type: Boolean,
    default: false
  },
  lastUpdated: {
    type: Date,
    default: Date.now
  },
  createdOn: {
    type: Date,
    default: Date.now
  },
  status: {
    type: String,
    enum: ['presented', 'entered', 'hold', 'paid',
      'partially paid', 'reversed', 'void'],
    required: true
  },
  invoiceItems: [InvoiceItemSchema]
});



var InvoiceItemSchema = new db.Schema({
  invoiceId: {
    type: db.Schema.ObjectId,
    required: true
  },
  description: String,
  qty: Number,
  rate: Number,
  isFlatFee: {
    type: Boolean,
    default: false
  }
});

I am able to create a new invoiceItem and push it directly into the invoice invoiceItems array, and read it when it's in there, but I am having a lot of trouble trying to update and delete. I was looking at this site here

http://tech-blog.maddyzone.com/node/add-update-delete-object-array-schema-mongoosemongodb

But I couldn't seem to get any of that to work.

I know in that blog he is using $set to update things but it looks like it only updates one field and I want to update any field based on user input and not a hard coded field. I don't have any code to show right now because I am quite lost but some help would be appreciated!

sunilsmith
  • 111
  • 2
  • 15
rdeg
  • 169
  • 3
  • 15

1 Answers1

0

If you have a reference to the Invoice from your InvoiceItem you shouldn't really need to save the items in a list in Invoice. If you want to get that list you could simple do a query like:

InvoiceItem.find({invoiceId: invoiceId}, function(err, items) {
   ...
})

Now this will make it much easier to update as you only need to update each InvoiceItem object and just keep the reference to the Invoice.

Hope this helped you out.

Mattias Farnemyhr
  • 4,148
  • 3
  • 28
  • 49
  • the idea is to have an invoice document that contains all the invoice-items. Since no invoice-item can live outside of an invoice, why should a separate invoice-item document be created (which I think is what you are suggesting)? – Kianosh Jun 23 '15 at 18:16
  • because it will make it much easier to maintain than having them in an array, like said, being able to update a particular item in a list is a pain. this solution shouldn't be viewed as living outside the invoice, it should always have a required reference to an invoice and you can use the standard mongoose functions to access it as such.. – Mattias Farnemyhr Jun 23 '15 at 18:44
  • But I thought the whole idea behind a NoSQL DB is to compartmentalize all the information in a retrievable format vs. a traditional relational DB. I don't disagree with your suggestion, I just think it is going against the advantages of a NoSQL DB. – Kianosh Jun 23 '15 at 21:23
  • Sure, I agree, but I tend to stay away from too complicated schemas as well. I will use nested dictionaries a lot because you can easily access and query them but with list where you want to update the content and access it a lot I usually use the "relation pattern" if you will. if the array contained just strings or simple dictionaries I would have it belong to the schema. – Mattias Farnemyhr Jun 23 '15 at 21:28