1

so I have define an end_time field in my document as

end_time: {
        type: Date
    },`

So when I update it to

    end_time: ''

It gets stored as null but when I display it to user it gives

06 march 1970 // which i guess is the default date

So how to set a null field so that I don't have such problems

1 Answer 1

2

if you want to remove a property from a mongo document you can use '$unset' when updating the object:

using mongoose the command would look like:

collection.update({_id: documentYouWant}, {$unset: {properyToUnset: true}}).exec(function(err, result) {
    if (err) {
        //handle error
    }
    // handle response
    return result;
});

You should also be able to update a property by setting the value to undefined or to null in the application and saving the object as long as the property is not required.

Sign up to request clarification or add additional context in comments.

6 Comments

I am using findOneAndUpdate and it seems it does not work with that
are you using mongo directly or mongoose?
mongoose something like this mBooking.findOneAndUpdate(updateDbParams.condition, updateDbParams.update, {$unset: {end_time: true}}, function(error, booking_result) { // console.log(error); //
{$unset: {end_time: true}} would be your update. Unfortunatly you cannot run a set and an unset at the same time so it would require 2 calls to do this if you want to update other properties as well
and for future reference, with update/findOneAndUpdate the params will be collection.update(query, update, options, callback) OR collection.update(query, update, options).exec(callback)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.