I am using mongodb in my application as backend database. And trying to update the sub-documents of a document with different values using mongoose.
Below is the sample schema for the document:
'use strict';
const mongoose = require('mongoose');
const activitiesSchema = new mongoose.Schema({
activityId : {
type: String
},
name: {
type: String,
},
points : {
type : String
}
});
const sampleSchema = new mongoose.Schema(
{
userId: {
type: String,
require: true,
},
userName: {
type : String,
},
activities: {
type: [activitiesSchema],
},
createdTime: {
type : Date,
default: Date.now,
}
},
);
const Sample = mongoose.model('Samples', sampleSchema, 'Samples');
module.exports = Sample;
I want to update the name to run if the activityId is 001 and, if the activity id is 002 I want the name to be update to walk.
Is this possible to do in single database find and update operation ?
Thank you,
KK