seppevs / migrate-mongo

A database migration tool for MongoDB in Node
MIT License
927 stars 163 forks source link

BSON field 'update.updates.u' is the wrong type 'array', expected type 'object' #204

Closed priyank6796 closed 4 years ago

priyank6796 commented 4 years ago

Describe the bug I am trying to update my collection using mongo update query with aggregation which is supported by Mongo version 4.0 and higher. I tried to run the migration using this tool but I have got the error

ERROR: Could not migrate down 20200504124656-remove_deleted_messsage.js: BSON field 'update.updates.u' is the wrong type 'array', expected type 'object'

Migration script

module.exports = {
  async up(db, client) {
    await db.collection('messages').updateMany(
        { is_deleted: true, archived: { $exists: false } },
        [
            {"$set": {"archived.content": "$content", "archived.url": "$url"}},
            {"$unset": ["content", "url"]  }
        ])
  },

  async down(db, client) {

  }
};

To Reproduce

Expected behavior

image

Additional context I am able to run the same script from mongo client shell. But gives error with migration tool

seppevs commented 4 years ago

I cannot reproduce this. I followed exactly the steps you mentioned. Do you run migrate-mongo up with the CLI?

If not: just a wild guess, but are you using the migrate-mongo API in a TypeScript project? It might be an issue with the migrate-mongo types provided by the DefinitelyTyped project. This is not something I maintain.

jamesholcomb commented 4 years ago

Updates with aggregation pipeline is a 4.2 feature:

https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/

seppevs commented 4 years ago

I received no replies from OP, so I'm closing this. Please feel free to reopen when necessary.