-3

I am trying to update datatype of a column in mongodb dataset, but even after successful run of command expected changes are not reflecting in compass.

query:

db.EquityPrice.find().forEach(function(data) {
    db.EquityPrice.update({_id : doc._id}, {$set : {Trade_Date : new Date(doc.Trade_Date) }});
})

Can anyone please help to figure out where I am going wrong?

Shashi Shankar
  • 859
  • 2
  • 8
  • 25
  • 1
    Is `doc._id` defined? Do you want to use `data['_id']` instead? – Matthias Herrmann Jun 09 '18 at 18:45
  • Someone put in significant effort to answer [a recent question of yours](https://stackoverflow.com/questions/50777936/can-we-use-singleton-class-as-an-example-for-singleton-pattern-design-questions), one that you've promptly deleted.... why? – Hovercraft Full Of Eels Jun 09 '18 at 20:12
  • @HovercraftFullOfEels...my mistake that I tried to frame a concern as question. But at the same time whatever mentioned in answer was already part of my question and I don't agree with answer. For example why Serialization or Cloning is issue with Singleton if I do not specify class implements Serializable or Clonable interface. So instead of getting into arguments I thought better to delete the questions. Apologies for that. – Shashi Shankar Jun 10 '18 at 15:01

1 Answers1

0
db.EquityPrice.find().forEach(function(data) {
    db.EquityPrice.update({_id : doc._id}, {$set : {Trade_Date : new Date(doc.Trade_Date) }});
})

In your code you are finding all entries in the collection EquityPrice and forEach element applying a function which has the document (data).

The update function which is called for each document expects a query param as first argument. In your case you pass {_id: doc._id} - but the object docis not defined and therefore undefined. The query doesn't find an element with the _id of undefined.

You want to update the passed data object - therefore change your code to:

db.EquityPrice.update({_id : data['_id']}, {$set : {Trade_Date : new Date(data['Trade_Date']) }});
Matthias Herrmann
  • 2,650
  • 5
  • 32
  • 66