mongoose-history icon indicating copy to clipboard operation
mongoose-history copied to clipboard

not working with findOneAndUpdate with $push

Open Mobasher-NetLinks opened this issue 6 years ago • 3 comments

I have the following query myModel.findOneAndUpdate({_id: ID},{$push: {myField: myData}},{$new: true}) But pre findOneAndUpdate not work and error occur in its integration with schema:

let options = {
  diffOnly: true,
  metadata: [
    {key: 'user', value: function(origin, d){
      let createdAt = new Date(d.createdAt).getTime();
      let updatedAt = new Date(d.updatedAt).getTime;
      if (createdAt < updatedAt || !operation) {
        operation = 'u';
      }
      return {
        id: operation === 'i' ? d.createdBy.id : d.updatedBy.id,
        firstName: operation === 'i' ? d.createdBy.firstName : d.updatedBy.firstName,
        lastName: operation === 'i' ? d.createdBy.lastName : d.updatedBy.lastName
      }
    }}
  ]
};
schema.plugin(mongooseHistory, options);

Error is in d.createdBy, because if I console d, it shows following data: { updatedAt: 2019-01-30T08:52:43.770Z, __v: undefined }

Mobasher-NetLinks avatar Jan 30 '19 08:01 Mobasher-NetLinks

It also not detect the changes in document, when we use $push and $pull

Mobasher-NetLinks avatar Feb 05 '19 07:02 Mobasher-NetLinks

I have a similar problem:

Error: key $push must not start with '$' at serializeInto (/app/node_modules/bson/lib/bson/parser/serializer.js:912:19) at serializeObject (/app/node_modules/bson/lib/bson/parser/serializer.js:348:18) at serializeInto (/app/node_modules/bson/lib/bson/parser/serializer.js:938:17) at serializeObject (/app/node_modules/bson/lib/bson/parser/serializer.js:348:18) at serializeInto (/app/node_modules/bson/lib/bson/parser/serializer.js:728:17) at serializeObject (/app/node_modules/bson/lib/bson/parser/serializer.js:348:18) at serializeInto (/app/node_modules/bson/lib/bson/parser/serializer.js:938:17) at BSON.serialize (/app/node_modules/bson/lib/bson/bson.js:63:28) at Query.toBin (/app/node_modules/mongodb-core/lib/connection/commands.js:143:25) at serializeCommands (/app/node_modules/mongodb-core/lib/connection/pool.js:1045:43) at Pool.write (/app/node_modules/mongodb-core/lib/connection/pool.js:1261:3) at WireProtocol.command (/app/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:186:12) at executeWrite (/app/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:301:11) at WireProtocol.insert (/app/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:18:5) at Server.insert (/app/node_modules/mongodb-core/lib/topologies/server.js:789:35) at Server.insert (/app/node_modules/mongodb/lib/topologies/topology_base.js:321:25)

aexei avatar Jun 04 '19 20:06 aexei

Any update on this issue?

chaudharykiran avatar Nov 18 '19 17:11 chaudharykiran