I am working on MongoDB for update object value which is inside of the array in MongoDB collection.
My collection is like
{
"_id": ObjectId("59b7e839200a5c00ee2d2851"),
"player": "New",
"playesList": [
{
"_id": ObjectId("59b2a4f749fee40959e556d3"),
"name": "abcd",
},
{
"_id": ObjectId("59b2a4f749fee40959e556d4"),
"name": "pqrs",
}
]
}
Now I want to update the name of the player whose id is 59b2a4f749fee40959e556d3
(i mean first player name currently it was abcd), whose collection id is 59b7e839200a5c00ee2d2851
and player New
.
So I am trying to update with this query
play.update(
{
'_id': '59b7e839200a5c00ee2d2851',
'player': 'new',
'playesList._id': '59b2a4f749fee40959e556d3'
},
{
'$set': { 'playesList.$.name': 'wxyz' }
},
function(error, success) {
console.log(error, success);
}
)
But here I got in console like null { ok: 1, nModified: 0, n: 0 }
and value cann't update into collection.
Please help me how can solve this error.
Thank you in advance.
Objects (documents) inside the array in MongoDB collection are called - subdocuments
In this case, to update specific subdocument with its own _id
, you can use Mongoose findOneAndUpdate method:
play.findOneAndUpdate({
"_id": "59b7e839200a5c00ee2d2851",
"playesList._id": "59b2a4f749fee40959e556d3"
}, {
"$set": {
"playesList.$.name": "something"
}
}, function(error, success) {
})
first you are need to find document in collection with:
"_id": "59b7e839200a5c00ee2d2851"
then find subdocument by its _id
using second parameter:
"playesList._id": "59b2a4f749fee40959e556d3"
and when you find the subdocument that you want to update, use $set operator to set new value to name
property of found subdocument:
"$set": {
"playesList.$.name": "something"
}
Note that findOneAndUpdate
returns previous state of updated document.
Working example:
var express = require('express')
var app = express()
var router = require('express').Router()
var mongoose = require('mongoose')
var Schema = mongoose.Schema
mongoose.connect('mongodb://localhost:27017/stackoverflowanswer')
mongoose.Promise = global.Promise
var PlayerSchema = new Schema({
play: String,
playersList: [{
name: String
}]
})
var Player = mongoose.model('Players', PlayerSchema)
app.use('/', router)
router.get('/add-player', function(req, res, next) {
var player = new Player()
player._id = "59b7e839200a5c00ee2d2851"
player.play = "New"
player.playersList.push({
_id: "59b2a4f749fee40959e556d3",
name: "abcd"
}, {
_id: "59b2a4f749fee40959e556d4",
name: "pqrs"
})
player.save(function(err) {
if (err) throw err
res.json({
message: 'Success'
})
})
})
router.get('/update-player', function(req, res, next) {
Player.findOneAndUpdate({
"_id": "59b7e839200a5c00ee2d2851",
"playersList._id": "59b2a4f749fee40959e556d3"
}, {
"$set": {
"playersList.$.name": "wxyz"
}
}, function(error, success) {
if (error) throw error
res.json({
message: 'Success'
})
})
})
app.listen(8080, function() {
console.log('Node.js listening on port ' + 8080)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With