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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived