0

I'm trying to add sub-id to my array elements but I get an error when I try to save this object using Robomongo:

{
  "_id": ObjectId("540098e2c9502ce88820bfc3"),
  "foo": "bar",
  "objects": [
    {
      "_id": new ObjectId(),
      "name": "a"
    },
    {
      "_id": new ObjectId(),
      "name": "b"
    }
  ]
}

Error:

"new" keyword not followed by Date constructor.

What am I doing wrong?

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
  • Umm `ObjectID` should be `ObjectId()`. Thankyou typing correction. – Neil Lunn Sep 01 '14 at 13:07
  • it's just a typo here, the problem is not this one. – Fez Vrasta Sep 01 '14 at 13:29
  • More code please. Mongoose defines an `_id` implicitly on array elements by default. But possibly guessing you never imported `ObjectId()` anywhere to start with. If not, show more code for context. – Neil Lunn Sep 01 '14 at 13:39
  • I'm not using Mongoose, I'm just trying to assign ObjectIds to my array elements, there's not "more code". – Fez Vrasta Sep 01 '14 at 13:46
  • I think your "typo" **was** the problem. If I cut and paste your structure as shown now into RoboMongo, then it creates a new document as would be expected. So just giving you the chance to admit your mistake before the flood of downvotes come in. Fair play. – Neil Lunn Sep 01 '14 at 13:58
  • No, the problem is not the typo, I've just tried again to be sure and the error is the same. – Fez Vrasta Sep 01 '14 at 14:11
  • If I copy and paste the above into `XXX` in `db.test.insert(XXX)`, it works fine in the mongo shell. Furthermore, there's no `Date` constructor or anything like that obviously in the code, so I think the error must be coming from some other piece of code. What is the exact statement you are executing in the shell when you get this error? – wdberkeley Sep 02 '14 at 02:24
  • I'm not using the shell, I'm using Robomongo, I just copy and paste the document and press save. – Fez Vrasta Sep 02 '14 at 06:47
  • So there's a problem with RoboMongo, perhaps. Can you get the statement being sent by RoboMongo? – wdberkeley Sep 02 '14 at 15:49
  • I've no idea on how to do it.. – Fez Vrasta Sep 03 '14 at 06:52

0 Answers0