Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

not working with findOneAndUpdate with $push #51

Open
Mobasher-NetLinks opened this issue Jan 30, 2019 · 3 comments
Open

not working with findOneAndUpdate with $push #51

Mobasher-NetLinks opened this issue Jan 30, 2019 · 3 comments

Comments

@Mobasher-NetLinks
Copy link

Mobasher-NetLinks commented Jan 30, 2019

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
Copy link
Author

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

@aexei
Copy link

aexei commented Jun 4, 2019

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)

@chaudharykiran
Copy link

Any update on this issue?

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

No branches or pull requests

3 participants