1

I want to update my existing record with a new value in MongoDB, here I don't want to remove old value

mongodb record

here you can see that I have punching detail records and I want to add every time machine number and punching time

here is my model this model contains puching detail class

now I want to add new value every time there will be a puch in machine and can anyone have a solution?

ilkerkaran
  • 4,214
  • 3
  • 27
  • 42
Chitra Nandpal
  • 413
  • 4
  • 24
  • possible duplicate of https://stackoverflow.com/questions/7714216/add-new-field-to-every-document-in-a-mongodb-collection – Ali Kazmi Jul 31 '18 at 07:01

2 Answers2

0

You need to use Array data type for punching detail field and use MongoDB $push operator with update command.

For More Info: https://docs.mongodb.com/manual/reference/operator/update/push/

Hope it helps.

Zeeshan Tariq
  • 604
  • 5
  • 10
0

I will recommended to use Different Model for Punching Details and use mongodb ObjectId as a reference of Punching machine model. You can also create a Array and append new document on each update but it will cause performance issue when your array is too large. So you can use Punching_detail model as mentioned below

public class punching_detail {
   public string punchingModel_id {get;set;}
   public int machine_number {get;set;}
   public DateTime punching_time {get;set;}
}
Krunal Sonparate
  • 1,122
  • 10
  • 29