mongodb - Getting error when converting string to object id in mongo db -
i have collection booking
. in collection have field user
. there 24000 rows in collection. a-present user
field data type string. need update user
datatype string
objectid
. tried below script getting error. mongo db version 3.2.12
> db.booking.find({user: {$exists:true}}).foreach( function(x) { ... x.user = objectid(x.user); ... db.booking.update({_id: x._id}, {$set: {user: x.user}}); ... }); 2017-09-12t10:05:30.092+0000 e query [thread1] error: invalid object id: length : @(shell):2:14 dbquery.prototype.foreach@src/mongo/shell/query.js:501:1 @(shell):1:1 >
booking collection
{ "_id" : objectid("58046a49f8f888a80b00002a"), "cabinname" : "matras-haus", "checkin_from" : isodate("2016-10-17t00:00:00z"), "reserve_to" : isodate("2016-10-20t00:00:00z"), "user" : "57877d23049ac1b819000029", "beds" : "2", "dormitory" : "1", "sleeps" : "3", "clubmember" : "1", "status" : "4", "comments" : "", "bookingdate" : isodate("2016-10-17t06:06:01z"), "reservation_cancel" : "2", "is_delete" : numberlong(1), "payment_status" : "1" } { "_id" : objectid("58183678d2ae67a404431d5c"), "cabinname" : "kemptner hütte", "checkin_from" : isodate("2016-10-31t23:00:00z"), "reserve_to" : isodate("2016-11-23t23:00:00z"), "user" : "57877d23049ac1b819000029", "beds" : "2", "dormitory" : "0", "sleeps" : "2", "clubmember" : "0", "status" : "1", "total_price" : "1288", "payon_cabin" : "1288", "bed_prefer" : "0", "guests" : "2", "comments" : "", "prepayment_amount" : "0", "bookingdate" : isodate("2016-11-01t06:30:16z"), "reservation_cancel" : "2", "is_delete" : numberlong(1), "payment_status" : "0" } { "_id" : objectid("581b31f3d2ae674d5f431d5b"), "cabinname" : "kemptner hütte", "checkin_from" : isodate("2016-11-07t23:00:00z"), "reserve_to" : isodate("2016-11-17t23:00:00z"), "user" : "", "beds" : "3", "dormitory" : "0", "sleeps" : "3", "clubmember" : "0", "status" : "1", "total_price" : "840", "payon_cabin" : "840", "bed_prefer" : "0", "guests" : "3", "comments" : "", "prepayment_amount" : "0", "bookingdate" : isodate("2016-11-03t12:47:47z"), "reservation_cancel" : "2", "is_delete" : numberlong(1) } { "_id" : objectid("5821af65d2ae67c82154efc5"), "cabinname" : "kemptner hütte", "checkin_from" : isodate("2017-09-05t22:00:00z"), "reserve_to" : isodate("2018-01-24t23:00:00z"), "user" : "57877d23049ac1b819000029", "sleeps" : "2", "clubmember" : "0", "status" : "1", "total_price" : "5640", "payon_cabin" : "5630", "bed_prefer" : "0", "guests" : "2", "comments" : "", "prepayment_amount" : "21.25", "bookingdate" : isodate("2016-11-08t10:56:37z"), "reservation_cancel" : "2", "is_delete" : numberlong(1) } { "_id" : objectid("582558d4d2ae679c4d8b4567"), "cabinname" : "2", "checkin_from" : isodate("2017-07-31t22:00:00z"), "reserve_to" : isodate("2017-08-02t22:00:00z"), "user" : "57877d23049ac1b819000029", "beds" : "", "dormitory" : "", "sleeps" : "2", "clubmember" : "0", "status" : "", "total_price" : "80", "payon_cabin" : "60", "halfboard" : "", "bed_prefer" : "0", "guests" : "2", "prepayment_amount" : "20", "bookingdate" : isodate("2016-11-11t05:36:20z"), "is_delete" : numberlong(1) } { "_id" : objectid("58352c3cd2ae672341ec89e1"), "cabinname" : "kemptner hütte", "checkin_from" : isodate("2017-05-31t22:00:00z"), "reserve_to" : isodate("2017-06-02t22:00:00z"), "user" : "", "beds" : "", "dormitory" : "", "sleeps" : "2", "clubmember" : "0", "status" : "", "total_price" : "80", "payon_cabin" : "60", "halfboard" : "", "bed_prefer" : "0", "guests" : "2", "prepayment_amount" : "20", "bookingdate" : isodate("2016-11-23t05:42:20z"), "reservation_cancel" : "2", "is_delete" : numberlong(1) }
try this:
db.booking.find({user: {$exists:true}}).foreach( function(x) { x.user = new objectid(x.user); db.booking.save(x); });
however, user
value must unique hexadecimal string else fail.
Comments
Post a Comment