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
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
add a comment |
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
1
mongoose.set("debug", true)
and watch the issued queries/updates. Usingsave()
is a bad pattern anyway. Instead offind()
then "alter in code" thensave()
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 usingsave
: "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
add a comment |
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
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
mongoose
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. Usingsave()
is a bad pattern anyway. Instead offind()
then "alter in code" thensave()
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 usingsave
: "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
add a comment |
1
mongoose.set("debug", true)
and watch the issued queries/updates. Usingsave()
is a bad pattern anyway. Instead offind()
then "alter in code" thensave()
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 usingsave
: "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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
mongoose.set("debug", true)
and watch the issued queries/updates. Usingsave()
is a bad pattern anyway. Instead offind()
then "alter in code" thensave()
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