Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$rename throws cast error #1845

Closed
klimashkin opened this issue Dec 22, 2013 · 2 comments
Closed

$rename throws cast error #1845

klimashkin opened this issue Dec 22, 2013 · 2 comments
Milestone

Comments

@klimashkin
Copy link

Hello!
When I try to do simple $rename update I get "Cast to number error".
Photo.update({r2: 167}, {$rename: {r2: 'r1'}}, {multi: true})

But if I do this through Model.collection.update or through mongo console all ok.
Photo.collection.update({r2: 167}, {$rename: {r2: 'r1'}}, {multi: true})

@jrvidal
Copy link

jrvidal commented Mar 24, 2015

Apparently, Query.prototype._castUpdate casts the new field name to the corresponding type for $rename queries. Here is a simple test case:

var mongoose = require('mongoose'),

    User = mongoose.model('User', new mongoose.Schema({
        cool: Boolean
    }));

User.update({}, {
    $rename: {
        cool: 'awesome'
    }
}, function (err) {
    console.log(err.stack);
});

It failed for me in 3.8.25:

MongoError: The 'to' field for $rename must be a string: cool: true
    at Object.toError (/test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
    at /test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/core.js:577:27
    at /test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1195:7
    at /test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1903:9
    at Server.Base._callHandler (/test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
    at /test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
    at MongoReply.parseBody (/test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:445:20)
    at emit (events.js:95:17)
    at null.<anonymous> (/test_folder/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)

@sitegui
Copy link

sitegui commented May 8, 2015

It also seems to fail in [email protected]

Order.update({
    ETA: {
        $ne: null
    }
}, {
    $rename: {
        ETA: 'deliveryETA'
    }
}, {
    multi: true
}, done)
CastError: Cast to date failed for value "deliveryETA" at path "ETA"
    at SchemaDate.cast (node_modules\mongoose\lib\schema\date.js:232:9)
    at SchemaDate.castForQuery (node_modules\mongoose\lib\schema\date.js:277:17)
    at Query._castUpdateVal (node_modules\mongoose\lib\query.js:2241:17)
    at Query._walkUpdatePath (node_modules\mongoose\lib\query.js:2190:25)
    at Query._castUpdate (node_modules\mongoose\lib\query.js:2075:23)
    at Query.update (node_modules\mongoose\lib\query.js:1916:22)
    at Function.update (node_modules\mongoose\lib\model.js:1730:13)

Using Order.collection.update() works.

Is this intended? If yes, mongoose may want to warn about this in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants