Update using positional operator ($) in mongoose - mongodb

I have a document containing an array of objects. I wanted to update a particular element in the array. Tried using MongoDB shell, it works fine. But when I use in Mongoose in NodeJs, it is not working. The command is same in both the cases.
NodeJs code
const updateAttendance = await classModel.updateOne(
{
_id: item.classId,
'studentAttendance.studentId': item.studentId,
},
{ $set: { 'studentAtendance.$.present': true } }
)
Schema defination
const mongoose = require('mongoose')
const moment = require('moment')
const student = mongoose.Schema({
studentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
unique: true,
},
present: {
type: Boolean,
default: false,
},
})
const classes = mongoose.Schema({
date: {
type: String,
required: true,
default: moment().format('DD/MM/YYYY'),
validate: {
validator: (value) => {
return moment(value, 'DD/MM/YYYY', true).isValid()
},
message: 'Provide a valid date in the format of DD/MM/YYYY',
},
},
courseId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Course',
},
studentAttendance: [
{
type: student,
},
],
})
module.exports = mongoose.model('Class', classes)
Sample data
{
"date": "20/06/2021",
"_id": "60cf5446970dc063e40356d3",
"courseId": "60ce2c3aca275c868089ac48",
"studentAttendance": [
{
"present": false,
"_id": "60cf5446970dc063e40356d4",
"studentId": "60ce315f9f83a24544414705"
},
{
"present": false,
"_id": "60cf5446970dc063e40356d5",
"studentId": "60ce31ba9f83a2454441470a"
},
{
"present": false,
"_id": "60cf5446970dc063e40356d6",
"studentId": "60ce38e49f83a24544414712"
}
],
"__v": 0
}
What am I doing wrong or where is the problem?

Without looking at the schema def, just taking a punt in the dark that you dont explicitly say its an ObjectId.
Easy solve, just wrap "item.studentId" in mongoose.Types.ObjectId().
So your new code would be like
const updateAttendance = await classModel.updateOne({
_id: mongoose.Types.ObjectId(item.classId),
'studentAttendance.studentId': mongoose.Types.ObjectId(item.studentId),
},
{ $set: { 'studentAtendance.$.present': true } }
)
Don't forget const mongoose = require('mongoose');
Based on the update your update statement needs 'updating'. try fixing the spelling of studentAttendance vs studentAtendance in the $set statement.

Related

Is there any option that returns newly updated or created document with full object instead of count in mongoose bulkWrite() method?

const operations = [
{
updateOne: {
filter: { email: 'email.1#gmail.com' },
update: {
firstName: 'firstName.1',
lastName: 'lastName.1',
password: 'Password',
},
upsert: true,
},
},
{
updateOne: {
filter: { email: 'email.2#gmail.com' },
update: {
firstName: 'firstName.2',
lastName: 'lastName.2',
password: 'Password',
},
upsert: true,
},
},
{
updateOne: {
filter: { email: 'email.3#gmail.com' },
update: {
firstName: 'firstName.3',
lastName: 'lastName.3',
password: 'Password',
},
upsert: true,
},
},
];
I have above operations that I want to perform inside bulkWrite operation.
try {
const bulkWriteRes = await users.bulkWrite(operations);
console.log(bulkWriteRes);
} catch (err) {
console.log(err);
}
But inside bulkWriteRes I;m getting only these details.
BulkWriteResult {
ok: 1,
writeErrors: [],
writeConcernErrors: [],
insertedIds: [],
nInserted: 0,
nUpserted: 1,
nMatched: 3,
nModified: 3,
nRemoved: 0,
upserted: [
{
index: 5,
_id: '63c977188eb489a9c9cfa4e2',
},
],
};
I want whole updated object whether it is created or update including filed _id, fields that I have passed in filter and update properties. Is there any options ?
You can put all your updates as individual documents and store them as a new collection first(says toBeUsers). Then, you can perform a $merge to upsert the records into users collection.
db.toBeUsers.aggregate([
{
"$merge": {
"into": "users",
"on": "email",
"whenMatched": "merge",
"whenNotMatched": "insert"
}
}
])
Mongo Playground

Look up and create or update object inside array

I am currently trying to setup a schema for custom Discord guild commands:
const GuildCommandsSchema = new mongoose.Schema({
_id: String,
commands: [
{
name: {
type: String,
unique: true,
required: true,
},
action: {
type: String,
required: true,
},
author: {
type: String,
required: true,
},
},
],
});
Is this ok, performancewise, or could I improve it?
I feel like Mongo would need to look through all commands, since it can't index any commands inside 'commands' even though 'name' is unique.
If that's fine, how can I access the values inside commands?
I would need to find the right command via 'name' if it exists, otherwise create it and add/update 'action' + 'author'.
I tried something like this:
const updatedCommand = await GuildCommands.findOneAndUpdate(
{ _id },
{
$set: {
[`commands.$[outer].name`]: name,
[`commands.$[outer].action`]: action,
[`commands.$[outer].author`]: author,
},
},
{
arrayFilters: [{ 'outer.name': name }],
}
);
Unfortunately that does not create commands if they don't exist.
Thanks for your help
aggregate
db.collection.update({},
{
$set: {
"commands.$[c].name": "1",
"commands.$[c].author": "1",
"commands.$[c].action": "1"
}
},
{
arrayFilters: [
{
"c.author": "34"
}
],
multi: true
})
mongoplayground
To answer my own question:
I changed my Schema to use Maps instead of Arrays for performance improvments and also better model management.
const GuildCommandsSchema = new mongoose.Schema(
{
_id: String,
commands: {
type: Map,
of: {
_id: false,
name: {
type: String,
required: true,
},
action: {
type: String,
required: true,
},
active: {
type: Boolean,
required: true,
default: true,
},
author: {
type: String,
required: true,
},
},
},
},
{ versionKey: false }
);
The new query to find and update/create a command is also better imo:
const findCommand = await GuildCommands.findOne({ _id });
if (!action) {
const getCommand = findCommand.commands.get(name);
if (getCommand) {
message.reply(getCommand.action);
} else {
message.reply(`Cannot find ${name}`);
}
} else {
findCommand.commands.set(name, {
name,
action,
author,
});
findCommand.save();
}

Return the actual document instead of ObjectId

So, I have a model called Drivers that receive a field called "user", which references a document from another model, like this:
const DriversSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
},
{
timestamps: true,
}
);
// ...
Querying the collection works as expected, here's an example:
Drivers.find({});
// returns ->
[
{
"name": "John",
"user": "5e43f8ad2fbb1d0035d5a154",
}
]
Is there a way to return the actual document represented by the 'user' field?
Thanks!

MongoDB - Find and update an array object

I'm trying to update an object but I´m getting problems to update it. Please check the scheme:
const personSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
teachers: [
{
name: {
type: String,
},
information: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'info',
}],
},
],
I need to update an information inside this scheme Im trying to do (created is the object created):
const person = await ctx.models.person
.findOneAndUpdate({
teachers: {
$elemMatch: {
teachers: {
_id: input.processId,
},
},
},
},
{
$push: {
teachers: {
information: created,
},
},
}, {
new: true,
});
First of all, as recommended here by MongoDb official docs, you don't need $elemMatch:
If you specify only a single condition in the $elemMatch expression, you do not need to use $elemMatch.
Besides that, I recommend use the dot notation to search. Your code will be like that:
const person = await ctx.models.person
.findOneAndUpdate({
"teachers._id" : input.processId
},
{
$push: {
teachers: {
information: created,
},
},
}, {
new: true,
});

How do I remove a specific item from model's array?

This is my user model with watched and watchLater arrays:
const UserSchema = new mongoose.Schema(
{
email: { type: String, required: true, unique: true },
watched: [{ type: Number }],
watchLater: [{ type: Number }],
},
{ timestamps: true },
)
I have this function where I want to remove id from watchLater and add to watched:
async addToWatched(id) {
const _id = this.getUserId()
return await this.store.User.findByIdAndUpdate(
{ _id },
// remove id from watchLater and add to watched,
{ new: true },
)
}
How do I do that?
Been reading docs a bit. This seems to work:
return await this.store.User.findByIdAndUpdate(
{ _id },
// remove id from watchLater and add to watched,
{ $pull: { watchLater: id }, $addToSet: { watched: id } },
{ new: true },
)