MongoDB Tutorial #14 - Update Operators

preview_player
Показать описание
Hey gang, in this tutorial I'll be showing you how we can use update operators to change our data. In particular, we'll take a look at the increment operator. Find a full list at:

----- COURSE LINKS:

---------------------------------------------------------------------------------------------

========== PSD to WordPress Playlist ==========

============== The Net Ninja =====================

================== Social Links ==================

Рекомендации по теме
Комментарии
Автор

to actually update more than 1 record, what worked for me was using the 3rd arg to specifiy multi as true: MarioChar.update({},  { $inc: {weight: 1} },  { multi: true })

idontwhy
Автор

I think there is a little misconception about how you check the updated weight. By using update operator you are assuming that, every record might have different than initially value, but then you just assert it with static value. What you could do is checking it with value of objects found before the incrementation

kubapastuszuk
Автор

Could someone clarify for me how the following works: we update the name to Luigi and we're still able to grab onto Mario and increment his weight; Robo shows the Mario record with an incremented weight, yet the Luigi update test also passed but there is no Luigi to be found. Thanks for your reply.

mizz
Автор

I've had an issue with this update test always causing an assertion. Upon further exploring why I've noticed that the saving test actually saves the data to the db after the delete collection method and before the it method of the update test. This resulted in 2 "Mario's" to be present in the server. Because the findOneAndUpdate only changes 1 of both, it might have changed the wrong entry. If you face a similar issue, try to use: updateMany instead to change all mario's into Luigi. I've tried to resolve the issue in a different way. But for the sake of the tutorial on MongoDb I've instead chosen to continue with the other videos.

TimothyCallemein
Автор

i wonder that we update mario to luigi and we searched name:'mario'. should it be named luigi ? why didnt it give error?

suflebuddy
Автор

First of all, thanks for the tutorial, but I have a question. What if I wanted to create more than one MarioChar, like Yoshi, Peach... what should I do than? I tried something like:

describe("Updating Record's at MongoDB", function() {

var mario
var peach
var yoshi

beforeEach(function(done) {
mario = new MarioChar({
name: "Mario",
weight: 80
})

peach = new MarioChar({
name: "Peach",
weight: 50
})

yoshi = new MarioChar({
name: "Yoshi",
weight: 70
})

let saveChars= async() => {
await mario.save()
await peach.save()
await yoshi.save()
}

saveChars().then(function() {
assert(mario.isNew === false)
assert(peach.isNew === false)
assert(yoshi.isNew === false)
done();
})
})

But I don't know if this approach is correct. If someone could help me out, I would be pleased :)

joaocosta
Автор

Can we talk about the release date of the 3rd book of the Kingkiller Chronicle series?

majesticsid
welcome to shbcf.ru