Skip to content

Instantly share code, notes, and snippets.

@BMartinos
Last active August 25, 2020 14:20
Show Gist options
  • Save BMartinos/d24dab6cb6d5b77600a5aeb990c0d40f to your computer and use it in GitHub Desktop.
Save BMartinos/d24dab6cb6d5b77600a5aeb990c0d40f to your computer and use it in GitHub Desktop.
Mongoose Patch History $push bug
import Promise, { join } from 'bluebird'
import mongoose, { Schema } from 'mongoose'
import patchHistory from 'mongoose-patch-history'
mongoose.Promise = Promise
const PostSchema = new Schema(
{
title: String,
tags: { type: [String], default: void 0 },
},
{ timestamps: true }
)
PostSchema.plugin(patchHistory, {
mongoose,
name: 'postPatches',
transforms: [name => name.toLowerCase(), () => 'post_history'],
includes: {
version: { type: Number, from: '__v' },
reason: { type: String, from: '__reason' },
user: { type: Object, from: '__user' },
},
})
PostSchema.virtual('user').set(function (user) {
this.__user = user
})
PostSchema.virtual('reason').set(function (reason) {
this.__reason = reason
})
let Post = mongoose.model('Post', PostSchema)
mongoose
.connect('mongodb://localhost/mongoose-patch-history', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
join(
Post.deleteMany({}),
Post.Patches.deleteMany({})
)
.then(() => () => {
console.log('all done...')
})
})
Post.create({ title: 'tagged1', tags: ['match'] })
.then(post =>
Post.updateMany(
{ _id: post._id },
{ $push: { tags: 'match2' } },
{ timestamps: false }
)
)
.then(() => Post.find({ title: 'tagged1' }))
.then(posts =>
posts[0].patches.find({ ref: posts[0]._id }).sort({ _id: 1 })
)
.then(patches => {
console.log(patches[1].ops)
})
.then(() => () => {
console.log("all completed fine")
})
.catch((err) => {
console.log("Error: ", err)
})
{
"scripts": {
"compile": "rm -rf lib/ && babel -d lib/ index.js",
"start": "npm run compile && node lib/index.js"
},
"dependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.22.0",
"bluebird": "^3.7.2",
"mongoose": "^5.6.2",
"mongoose-patch-history": "^1.4.0"
}
}
@BMartinos
Copy link
Author

BMartinos commented Aug 25, 2020

The above example taken from the repository test script does not yield the same result as in the tests.

This specific example tests the $push operation of MongoDB (v4.2) which is currently broken in the new version (v1.4.0)

Steps to reproduce:

  1. Create relevant files
  2. Install dependencies: npm i
  3. Run the script: npm start

Error being returned:

Error:  { MongoError: unknown top level operator: $push
    at MessageStream.messageHandler (/../mongoose-patch-history-test/node_modules/mongodb/lib/cmap/connection.js:266:20)
    at MessageStream.emit (events.js:198:13)
    at processIncomingData (/../mongoose-patch-history-test/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/../mongoose-patch-history-test/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at MessageStream.Writable.write (_stream_writable.js:299:11)
    at Socket.ondata (_stream_readable.js:710:20)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) ok: 0, code: 2, codeName: 'BadValue', name: 'MongoError' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment