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

Ignore _id in mongoose subdocuments #15

Closed
mauliksoneji opened this issue Aug 18, 2017 · 2 comments
Closed

Ignore _id in mongoose subdocuments #15

mauliksoneji opened this issue Aug 18, 2017 · 2 comments

Comments

@mauliksoneji
Copy link

I find this library really helpful in storing history of documents.

However, when there are any changes made to the document and we pass in a Javascript object, mongoose will replace the old _id properties in subdocument and replace it with new values.

For example:
I have a document as follows:

"completionCriteria": [
    {
        "formula" : {
            "quantifier" : "all"
        },
        "_id" : ObjectId("5996d4524d98c8001558c7b9"),
        "action" : "complete",
        "requiredCondition" : "complete"
    },
    {
        "formula" : {
            "quantifier" : "any"
        },
        "_id" : ObjectId("5996d4524d98c8001558c7b8"),
        "action" : "inComplete",
        "requiredCondition" : "inComplete"
    },
    {
        "formula" : {
            "quantifier" : "all"
        },
        "_id" : ObjectId("5996d4524d98c8001558c7b7"),
        "action" : "notAttempted",
        "requiredCondition" : "notAttempted"
    }
]

Now when I change the document without making any changes to the completionCriteria array, the array remains the same but the _id property array elements change and thus this gets mentioned in History document as follows:

"completionCriteria": {
    "0" : {
        "_id" : [ 
            "5996d4524d98c8001558c7b9", 
            "5996d4914d98c8001558c7d5"
        ]
    },
    "1" : {
        "_id" : [ 
            "5996d4524d98c8001558c7b8", 
            "5996d4914d98c8001558c7d4"
        ]
    },
    "2" : {
        "_id" : [ 
            "5996d4524d98c8001558c7b7", 
            "5996d4914d98c8001558c7d3"
        ]
    },
    "_t" : "a"
}

Is there any way to ignore the _id property because this is automatically generated by mongoose and thus can be ignored?

@mimani
Copy link
Owner

mimani commented Aug 19, 2017

Ideally _id should not had changed as there are no changes in the completionCriteria array. I see a similar question here, not sure it will help. In case if you don't need _id for every element in the array, If not you can disable it as explained here.

I have not added such feature to ignore some fields, but it can be done easily by adding propertyfilter in the diff function I am using here.

@mauliksoneji
Copy link
Author

Thanks for the early reply.

I haven't tried using the propertyFilter that you discussed above, but I wrote a function that would ignore _id property in JSON.stringify step as follows:

function replacer(key, value) {
  // Filtering out properties
  if (key === '_id') {
    return undefined;
  }
  return value;
}
const diff = jsondiffpatch.diff(JSON.parse(JSON.stringify(original, replacer)),
    JSON.parse(JSON.stringify(updated, replacer)));

Thanks again for the help, I am closing the issue.

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

2 participants