Mongoose Version error - how does this work?Mongoose difference between .save() and using update()How to paginate with Mongoose in Node.js?How do I update/upsert a document in Mongoose?What is the “__v” field in MongooseMongoose findByIdAndUpdate / findById is returning null as modelMongo/mongoose schema is being cached somewhereMongoose does not return _id in findOneMongoose save not actually saving model, even with markModifiedMongodb document saving don't work with nested SchemaNodeJS, TypeScript, Mongoose unable to get document with FindByIdConstant not reflected in function

Is it a bad idea to plug the other end of ESD strap to wall ground?

Should I tell management that I intend to leave due to bad software development practices?

How badly should I try to prevent a user from XSSing themselves?

One verb to replace 'be a member of' a club

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

How to show a landlord what we have in savings?

Why is the sentence "Das ist eine Nase" correct?

Is it inappropriate for a student to attend their mentor's dissertation defense?

How to stretch the corners of this image so that it looks like a perfect rectangle?

Do creatures with a listed speed of "0 ft., fly 30 ft. (hover)" ever touch the ground?

What are the G forces leaving Earth orbit?

How obscure is the use of 令 in 令和?

How dangerous is XSS

files created then deleted at every second in tmp directory

How do I exit BASH while loop using modulus operator?

Does the Idaho Potato Commission associate potato skins with healthy eating?

What is the most common color to indicate the input-field is disabled?

Avoiding the "not like other girls" trope?

Description list Formatting using enumitem

Notepad++ delete until colon for every line with replace all

Can a virus destroy the BIOS of a modern computer?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

Am I breaking OOP practice with this architecture?

Does Dispel Magic work on Tiny Hut?



Mongoose Version error - how does this work?


Mongoose difference between .save() and using update()How to paginate with Mongoose in Node.js?How do I update/upsert a document in Mongoose?What is the “__v” field in MongooseMongoose findByIdAndUpdate / findById is returning null as modelMongo/mongoose schema is being cached somewhereMongoose does not return _id in findOneMongoose save not actually saving model, even with markModifiedMongodb document saving don't work with nested SchemaNodeJS, TypeScript, Mongoose unable to get document with FindByIdConstant not reflected in function













2















I am trying to understand the version error. Every time I think I have a firm understanding of it, I find out I'm still off base. Can someone help me understand why resaving would still work?



context('when dealing with multiple updates to the same thing', () => 
let thing;
let thingA;
let thingB;
before(async () =>
thing = utils.generateThing();
await thing.save();
// Get the thing directly from the database (same thing, different object)
thingA = await models.Thing.findOne( '_id': thing._id );
thingB = await models.Thing.findOne( '_id': thing._id );
);

it('should handle the update', async () =>
let yupItSaved = false;
// Save modified thing to database (bumps the version)
thingA.set('propertyArray', ['Monday', 'Tuesday']);
await thingA.save();

// Then try and save thing object
thingB.set('propertyArray', ['Monday', 'Tuesday']);

try
thingB.__v.should.equal(0);
thingA.__v.should.equal(1);
await thingB.save();
should.fail(null, null, 'VersionError was not thrown');
catch (err)
// Expect the VersionError here since versions don't match
err.name.should.equal('VersionError');
const thingAfterError = await models.Thing.findOne( '_id': thing._id );
thingAfterError.__v.should.equal(1);
thingA.__v.should.equal(1);
thingB.__v.should.equal(0);
// Don't understand why this works even though versions still don't match
await thingB.save();
yupItSaved = true;

yupItSaved.should.equal(true);
);
);









share|improve this question



















  • 1





    mongoose.set("debug", true) and watch the issued queries/updates. Using save() is a bad pattern anyway. Instead of find() then "alter in code" then save() you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure.

    – Neil Lunn
    Mar 8 at 21:06












  • I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it.

    – loctrice
    Mar 10 at 18:20











  • Really? What exactly do you think is so difficult to change? Mongoose difference between .save() and using update(). Point is that you're probably "Barking up the wrong tree!" as the saying goes. The solution isn't "Make versions work", but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things.

    – Neil Lunn
    Mar 10 at 22:20












  • Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial.

    – loctrice
    Mar 11 at 11:19






  • 1





    @NeilLunn - The mongoose documentation seems to drive folks towards using save: "The save() function is generally the right way to update a document with Mongoose." (mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct?

    – MikeTheReader
    Mar 12 at 14:38















2















I am trying to understand the version error. Every time I think I have a firm understanding of it, I find out I'm still off base. Can someone help me understand why resaving would still work?



context('when dealing with multiple updates to the same thing', () => 
let thing;
let thingA;
let thingB;
before(async () =>
thing = utils.generateThing();
await thing.save();
// Get the thing directly from the database (same thing, different object)
thingA = await models.Thing.findOne( '_id': thing._id );
thingB = await models.Thing.findOne( '_id': thing._id );
);

it('should handle the update', async () =>
let yupItSaved = false;
// Save modified thing to database (bumps the version)
thingA.set('propertyArray', ['Monday', 'Tuesday']);
await thingA.save();

// Then try and save thing object
thingB.set('propertyArray', ['Monday', 'Tuesday']);

try
thingB.__v.should.equal(0);
thingA.__v.should.equal(1);
await thingB.save();
should.fail(null, null, 'VersionError was not thrown');
catch (err)
// Expect the VersionError here since versions don't match
err.name.should.equal('VersionError');
const thingAfterError = await models.Thing.findOne( '_id': thing._id );
thingAfterError.__v.should.equal(1);
thingA.__v.should.equal(1);
thingB.__v.should.equal(0);
// Don't understand why this works even though versions still don't match
await thingB.save();
yupItSaved = true;

yupItSaved.should.equal(true);
);
);









share|improve this question



















  • 1





    mongoose.set("debug", true) and watch the issued queries/updates. Using save() is a bad pattern anyway. Instead of find() then "alter in code" then save() you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure.

    – Neil Lunn
    Mar 8 at 21:06












  • I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it.

    – loctrice
    Mar 10 at 18:20











  • Really? What exactly do you think is so difficult to change? Mongoose difference between .save() and using update(). Point is that you're probably "Barking up the wrong tree!" as the saying goes. The solution isn't "Make versions work", but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things.

    – Neil Lunn
    Mar 10 at 22:20












  • Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial.

    – loctrice
    Mar 11 at 11:19






  • 1





    @NeilLunn - The mongoose documentation seems to drive folks towards using save: "The save() function is generally the right way to update a document with Mongoose." (mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct?

    – MikeTheReader
    Mar 12 at 14:38













2












2








2








I am trying to understand the version error. Every time I think I have a firm understanding of it, I find out I'm still off base. Can someone help me understand why resaving would still work?



context('when dealing with multiple updates to the same thing', () => 
let thing;
let thingA;
let thingB;
before(async () =>
thing = utils.generateThing();
await thing.save();
// Get the thing directly from the database (same thing, different object)
thingA = await models.Thing.findOne( '_id': thing._id );
thingB = await models.Thing.findOne( '_id': thing._id );
);

it('should handle the update', async () =>
let yupItSaved = false;
// Save modified thing to database (bumps the version)
thingA.set('propertyArray', ['Monday', 'Tuesday']);
await thingA.save();

// Then try and save thing object
thingB.set('propertyArray', ['Monday', 'Tuesday']);

try
thingB.__v.should.equal(0);
thingA.__v.should.equal(1);
await thingB.save();
should.fail(null, null, 'VersionError was not thrown');
catch (err)
// Expect the VersionError here since versions don't match
err.name.should.equal('VersionError');
const thingAfterError = await models.Thing.findOne( '_id': thing._id );
thingAfterError.__v.should.equal(1);
thingA.__v.should.equal(1);
thingB.__v.should.equal(0);
// Don't understand why this works even though versions still don't match
await thingB.save();
yupItSaved = true;

yupItSaved.should.equal(true);
);
);









share|improve this question
















I am trying to understand the version error. Every time I think I have a firm understanding of it, I find out I'm still off base. Can someone help me understand why resaving would still work?



context('when dealing with multiple updates to the same thing', () => 
let thing;
let thingA;
let thingB;
before(async () =>
thing = utils.generateThing();
await thing.save();
// Get the thing directly from the database (same thing, different object)
thingA = await models.Thing.findOne( '_id': thing._id );
thingB = await models.Thing.findOne( '_id': thing._id );
);

it('should handle the update', async () =>
let yupItSaved = false;
// Save modified thing to database (bumps the version)
thingA.set('propertyArray', ['Monday', 'Tuesday']);
await thingA.save();

// Then try and save thing object
thingB.set('propertyArray', ['Monday', 'Tuesday']);

try
thingB.__v.should.equal(0);
thingA.__v.should.equal(1);
await thingB.save();
should.fail(null, null, 'VersionError was not thrown');
catch (err)
// Expect the VersionError here since versions don't match
err.name.should.equal('VersionError');
const thingAfterError = await models.Thing.findOne( '_id': thing._id );
thingAfterError.__v.should.equal(1);
thingA.__v.should.equal(1);
thingB.__v.should.equal(0);
// Don't understand why this works even though versions still don't match
await thingB.save();
yupItSaved = true;

yupItSaved.should.equal(true);
);
);






mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 20:54









Neil Lunn

101k23178187




101k23178187










asked Mar 8 at 19:39









loctriceloctrice

1,04311126




1,04311126







  • 1





    mongoose.set("debug", true) and watch the issued queries/updates. Using save() is a bad pattern anyway. Instead of find() then "alter in code" then save() you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure.

    – Neil Lunn
    Mar 8 at 21:06












  • I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it.

    – loctrice
    Mar 10 at 18:20











  • Really? What exactly do you think is so difficult to change? Mongoose difference between .save() and using update(). Point is that you're probably "Barking up the wrong tree!" as the saying goes. The solution isn't "Make versions work", but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things.

    – Neil Lunn
    Mar 10 at 22:20












  • Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial.

    – loctrice
    Mar 11 at 11:19






  • 1





    @NeilLunn - The mongoose documentation seems to drive folks towards using save: "The save() function is generally the right way to update a document with Mongoose." (mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct?

    – MikeTheReader
    Mar 12 at 14:38












  • 1





    mongoose.set("debug", true) and watch the issued queries/updates. Using save() is a bad pattern anyway. Instead of find() then "alter in code" then save() you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure.

    – Neil Lunn
    Mar 8 at 21:06












  • I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it.

    – loctrice
    Mar 10 at 18:20











  • Really? What exactly do you think is so difficult to change? Mongoose difference between .save() and using update(). Point is that you're probably "Barking up the wrong tree!" as the saying goes. The solution isn't "Make versions work", but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things.

    – Neil Lunn
    Mar 10 at 22:20












  • Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial.

    – loctrice
    Mar 11 at 11:19






  • 1





    @NeilLunn - The mongoose documentation seems to drive folks towards using save: "The save() function is generally the right way to update a document with Mongoose." (mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct?

    – MikeTheReader
    Mar 12 at 14:38







1




1





mongoose.set("debug", true) and watch the issued queries/updates. Using save() is a bad pattern anyway. Instead of find() then "alter in code" then save() you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure.

– Neil Lunn
Mar 8 at 21:06






mongoose.set("debug", true) and watch the issued queries/updates. Using save() is a bad pattern anyway. Instead of find() then "alter in code" then save() you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure.

– Neil Lunn
Mar 8 at 21:06














I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it.

– loctrice
Mar 10 at 18:20





I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it.

– loctrice
Mar 10 at 18:20













Really? What exactly do you think is so difficult to change? Mongoose difference between .save() and using update(). Point is that you're probably "Barking up the wrong tree!" as the saying goes. The solution isn't "Make versions work", but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things.

– Neil Lunn
Mar 10 at 22:20






Really? What exactly do you think is so difficult to change? Mongoose difference between .save() and using update(). Point is that you're probably "Barking up the wrong tree!" as the saying goes. The solution isn't "Make versions work", but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things.

– Neil Lunn
Mar 10 at 22:20














Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial.

– loctrice
Mar 11 at 11:19





Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial.

– loctrice
Mar 11 at 11:19




1




1





@NeilLunn - The mongoose documentation seems to drive folks towards using save: "The save() function is generally the right way to update a document with Mongoose." (mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct?

– MikeTheReader
Mar 12 at 14:38





@NeilLunn - The mongoose documentation seems to drive folks towards using save: "The save() function is generally the right way to update a document with Mongoose." (mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct?

– MikeTheReader
Mar 12 at 14:38












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069932%2fmongoose-version-error-how-does-this-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069932%2fmongoose-version-error-how-does-this-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page