How to update two fields in one condition in mongodb? - node.js

What im trying to do is to update and push in two different fields when a condition is true, and to update only one field when the condition is false.
const updateQuery = historicEnable == true ? ({ $push: { locationHistoric: locationObject } }, {currentLocation: locationObject}) : ({ currentLocation: locationObject })
const theLocation = await User.findOneAndUpdate({ phone }, updateQuery, {
new: true,
});
By far when the condition is true, it pushes only in the locationHistoric, and not updating the currentLocation.
How can I make both of the methods work on the same condition?

Try this :
const updateQuery = historicEnable == true ?
({ $push: { "locationHistoric": locationObject, "currentLocation": locationObject }) :
({ $push: { "currentLocation": locationObject } })
And you're also missing operator in second statement.
Use any operator like $set or $push

Related

Update document inside array of objects in mongoose

I need to update objects inside an array so I'm trying but I get the following error:
error Plan executor error during findAndModify :: caused by :: The
positional operator did not find the match needed from the query.
This is my code:
const payment = await Purchase.findByIdAndUpdate(
{ '_id': req.body.id, 'payments._id': req.body.paymentId },
{
$set: {
'payments.$.status': false
}
}
,{ new: true });
payments object on Model:
payments: [
{
createdBy: [Object],
createdAt: '08/13/22',
paymentNumber: 0,
previousBalance: 3747.68,
paymentAmount: 3747.68,
outstandingBalance: 0,
status: true,
_id: new ObjectId("62f83f3c22e4f67dde8cb85a"),
lastModificationBy: [],
disabledBy: []
}
]
while using fineByIdAndUpdate you only need to pass id of document to be updated.
const payment = await Purchase.findByIdAndUpdate(req.body.paymentId,{status:false},{new:true} )
for using findByIdAndUpdate, you need to add this runValidators
const payment = await Purchase.findByIdAndUpdate(req.params.id,
'payments.$.status': false
}, {runValidators: true}
while same update can be done by this as well
const payment = await Purchase.findByIdAndUpdate(
req.body.paymentId,
{
status:false
}, {
new:true
}
)

MongoDB : Push new item to the top of the array via findOneandUpdate

I am a nodejs and mongo newbie. I want to push an item into the array which is nested into the document via findOneandUpdate, but would like to add the new item as the first element (top of the array). Here is my structure :
This works fine for adding item to the end of the array:
const newNote = await NotesBlock.findOneAndUpdate({ blockId: req.params.blockId }, { $push: { notes: { noteTitle: req.body.noteTitle, noteDetail: req.body.noteDetail } } }, { upsert: true, new: true })
As I know we can't use unshift with mongodb, but $each and $position can be used for this purpose. So, I have tried this :
const newNote = await NotesBlock.findOneAndUpdate({ blockId: req.params.blockId }, { $push: { notes: { $each:[noteTitle: req.body.noteTitle, noteDetail: req.body.noteDetail], $position: 0 } } }, { upsert: true, new: true })
But unfortunately, this gives me an error for the syntax.
I can't figure out how to avoid this and make it work. Or is this the wrong approach and there is any other way to achieve?
Thanks for the help
I have figured out the issue. Following was close enough, except the curly brackets that should be inside the square brackets. So I had to change this :
const newNote = await NotesBlock.findOneAndUpdate({ blockId: req.params.blockId }, { $push: { notes: { $each:[noteTitle: req.body.noteTitle, noteDetail: req.body.noteDetail], $position: 0 } } }, { upsert: true, new: true })
To this :
const newNote = await NotesBlock.findOneAndUpdate({ blockId: req.params.blockId }, { $push: { notes: { $each: [{ noteTitle: req.body.noteTitle, noteDetail: req.body.noteDetail }], $position: 0 } } }, { upsert: true, new: true })
And it works as expected now.

Concurrency problems updating another's collection stats

I'm trying to make a notation system for movies
A user can note a Movie in their List.
Whenever the user clicks on the frontend, the listId, movieId, note are sent to the server to update the note. The note can be set to null, but it does not remove the entry from the list.
But if the user clicks too much times, the movie's totalNote and nbNotes are completely broken. Feels like there is some sort of concurrency problems ?
Is this the correct approach to this problem or am I updating in a wrong way ?
The mongoose schemas related :
// Movie Schema
const movieSchema = new Schema({
// ...
note: { type: Number, default: 0 },
totalNotes: { type: Number, default: 0 },
nbNotes: { type: Number, default: 0 },
})
movieSchema.statics.updateTotalNote = function (movieId, oldNote, newNote) {
if (!oldNote && !newNote) return
const nbNotes = !newNote ? -1 : (!oldNote ? 1 : 0) // If oldNote is null we +1, if newNote is null we -1
return Movie.findOneAndUpdate({ _id: movieId }, { $inc: { nbNotes: nbNotes, totalNotes: (newNote - oldNote) } }, { new: true }).catch(err => console.error("Couldn't update note from movie", err))
}
// List Schema
const movieEntry = new Schema({
_id: false, // movie makes an already unique attribute, which is populated on GET
movie: { type: Schema.Types.ObjectId, ref: 'Movies', required: true },
note: { type: Number, default: null, max: 21 },
})
const listSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'Users', required: true },
movies: [movieEntry]
})
The server update API (add / Remove movieEntry are similar with $push and $pull instead of $set)
exports.updateEntry = (req, res) => {
const { listId, movieId } = req.params
const movieEntry = { movieId: movieId, note: req.body.note }
List.findOneAndUpdate({ _id: listId, 'movies.movie': movieId }, { $set: { 'movies.$[elem]': movieEntry } }, { arrayFilters: [{ 'elem.movie': movieId }] })
.exec()
.then(list => {
if (!list) return res.sendStatus(404)
const oldNote = list.getMovieEntryById(movieId).note // getMovieEntryById(movieId) = return this.movies.find(movieEntry => movieEntry.movie == movieId)
Movie.updateTotalNote(movieId, oldNote, movieEntry.note)
let newList = list.movies.find(movieEntry => movieEntry.movie == movieId) // Because I needed the oldNote and findOneAndUpdate returns the list prior to modification, I change it to return it
newList.note = movieEntry.note
newList.status = movieEntry.status
newList.completedDate = movieEntry.completedDate
return res.status(200).json(list)
})
.catch(err => {
console.error(err)
return res.sendStatus(400)
})
}
The entries I needed to update were arrays that could grow indefinitely so I had to first change my models and use virtuals and another model for the the list entries.
Doing so made the work easier and I was able to create, update and delete the entries more easily and without any concurrency problems.
This might also not have been a concurrency problem in the first place, but a transaction problem.

mongodb findOneAndUpdate replace document instead of updating specified field [duplicate]

Say, i have a document:
{
_id: 'some_mongodb_id',
name: 'john doe',
phone: '+12345678901',
}
I want to update this document:
.findOneAndUpdate({_id: 'some_mongodb_id'}, {name: 'Dan smith'})
And the result should be this:
{
_id: 'some_mongodb_id',
name: 'Dan smith',
}
The property, that is not specified, should be removed.
How do i do that?
Actually, but for the fact that mongoose is actually "messing with" the update under the covers, this is actually the default action of your submission to a regular MongoDB function.
So mongoose deems it "wise" as a convenience method to "presume" you meant to issue a $set instruction here. Since you actually do not want to do that in this case, you turn off that behavior via { overwrite: true } in the options passed to any .update() method:
As a full example:
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
const uri = 'mongodb://localhost/test',
options = { useMongoClient: true };
const testSchema = new Schema({
name: String,
phone: String
});
const Test = mongoose.model('Test', testSchema);
function log(data) {
console.log(JSON.stringify(data,undefined,2))
}
(async function() {
try {
const conn = await mongoose.connect(uri,options);
// Clean data
await Promise.all(
Object.keys(conn.models).map( m => conn.models[m].remove({}) )
);
// Create a document
let test = await Test.create({
name: 'john doe',
phone: '+12345678901'
});
log(test);
// This update will apply using $set for the name
let notover = await Test.findOneAndUpdate(
{ _id: test._id },
{ name: 'Bill S. Preston' },
{ new: true }
);
log(notover);
// This update will just use the supplied object, and overwrite
let updated = await Test.findOneAndUpdate(
{ _id: test._id },
{ name: 'Dan Smith' },
{ new: true, overwrite: true }
);
log(updated);
} catch (e) {
console.error(e);
} finally {
mongoose.disconnect();
}
})()
Produces:
Mongoose: tests.remove({}, {})
Mongoose: tests.insert({ name: 'john doe', phone: '+12345678901', _id: ObjectId("596efb0ec941ff0ec319ac1e"), __v: 0 })
{
"__v": 0,
"name": "john doe",
"phone": "+12345678901",
"_id": "596efb0ec941ff0ec319ac1e"
}
Mongoose: tests.findAndModify({ _id: ObjectId("596efb0ec941ff0ec319ac1e") }, [], { '$set': { name: 'Bill S. Preston' } }, { new: true, upsert: false, remove: false, fields: {} })
{
"_id": "596efb0ec941ff0ec319ac1e",
"name": "Bill S. Preston",
"phone": "+12345678901",
"__v": 0
}
Mongoose: tests.findAndModify({ _id: ObjectId("596efb0ec941ff0ec319ac1e") }, [], { name: 'Dan Smith' }, { new: true, overwrite: true, upsert: false, remove: false, fields: {} })
{
"_id": "596efb0ec941ff0ec319ac1e",
"name": "Dan Smith"
}
Showing the document is "overwritten" because we suppressed the $set operation that otherwise would have been interpolated. The two samples show first without the overwrite option, which applies the $set modifier, and then "with" the overwrite option, where the object you passed in for the "update" is respected and no such $set modifier is applied.
Note, this is how the MongoDB Node driver does this "by default". So the behavior of adding in the "implicit" $set is being done by mongoose, unless you tell it not to.
NOTE The true way to "replace" would actually be to use replaceOne, either as the API method of replaceOne() or through bulkWrite(). The overwrite is a legacy of how mongoose wants to apply $set as described and demonstrated above, however the MongoDB official API introduces replaceOne as a "special" king of update() operation which does not allow the usage of atomic operators like $set within the statement and will error if you try.
This is much clearer semantically since replace reads very clearly as to what the method is actually used for. Within standard API calls to the update() variants of course still allow you to omit the atomic operators and will just replace content anyway. But warnings should be expected.
You can pass upsert option, and it will replace document:
var collection = db.collection('test');
collection.findOneAndUpdate(
{'_id': 'some_mongodb_id'},
{name: 'Dan smith Only'},
{upsert: true},
function (err, doc) {
console.log(doc);
}
);
But the problem here - is that doc in callback is found document but not updated.
Hence you need perform something like this:
var collection = db.collection('test');
collection.update(
{'_id': 'some_mongodb_id'},
{name: 'Dan smith Only'},
{upsert: true},
function (err, doc) {
collection.findOne({'_id': 'some_mongodb_id'}, function (err, doc) {
console.log(doc);
});
}
);

Mongoose overwrite the document rather that `$set` fields

Say, i have a document:
{
_id: 'some_mongodb_id',
name: 'john doe',
phone: '+12345678901',
}
I want to update this document:
.findOneAndUpdate({_id: 'some_mongodb_id'}, {name: 'Dan smith'})
And the result should be this:
{
_id: 'some_mongodb_id',
name: 'Dan smith',
}
The property, that is not specified, should be removed.
How do i do that?
Actually, but for the fact that mongoose is actually "messing with" the update under the covers, this is actually the default action of your submission to a regular MongoDB function.
So mongoose deems it "wise" as a convenience method to "presume" you meant to issue a $set instruction here. Since you actually do not want to do that in this case, you turn off that behavior via { overwrite: true } in the options passed to any .update() method:
As a full example:
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
const uri = 'mongodb://localhost/test',
options = { useMongoClient: true };
const testSchema = new Schema({
name: String,
phone: String
});
const Test = mongoose.model('Test', testSchema);
function log(data) {
console.log(JSON.stringify(data,undefined,2))
}
(async function() {
try {
const conn = await mongoose.connect(uri,options);
// Clean data
await Promise.all(
Object.keys(conn.models).map( m => conn.models[m].remove({}) )
);
// Create a document
let test = await Test.create({
name: 'john doe',
phone: '+12345678901'
});
log(test);
// This update will apply using $set for the name
let notover = await Test.findOneAndUpdate(
{ _id: test._id },
{ name: 'Bill S. Preston' },
{ new: true }
);
log(notover);
// This update will just use the supplied object, and overwrite
let updated = await Test.findOneAndUpdate(
{ _id: test._id },
{ name: 'Dan Smith' },
{ new: true, overwrite: true }
);
log(updated);
} catch (e) {
console.error(e);
} finally {
mongoose.disconnect();
}
})()
Produces:
Mongoose: tests.remove({}, {})
Mongoose: tests.insert({ name: 'john doe', phone: '+12345678901', _id: ObjectId("596efb0ec941ff0ec319ac1e"), __v: 0 })
{
"__v": 0,
"name": "john doe",
"phone": "+12345678901",
"_id": "596efb0ec941ff0ec319ac1e"
}
Mongoose: tests.findAndModify({ _id: ObjectId("596efb0ec941ff0ec319ac1e") }, [], { '$set': { name: 'Bill S. Preston' } }, { new: true, upsert: false, remove: false, fields: {} })
{
"_id": "596efb0ec941ff0ec319ac1e",
"name": "Bill S. Preston",
"phone": "+12345678901",
"__v": 0
}
Mongoose: tests.findAndModify({ _id: ObjectId("596efb0ec941ff0ec319ac1e") }, [], { name: 'Dan Smith' }, { new: true, overwrite: true, upsert: false, remove: false, fields: {} })
{
"_id": "596efb0ec941ff0ec319ac1e",
"name": "Dan Smith"
}
Showing the document is "overwritten" because we suppressed the $set operation that otherwise would have been interpolated. The two samples show first without the overwrite option, which applies the $set modifier, and then "with" the overwrite option, where the object you passed in for the "update" is respected and no such $set modifier is applied.
Note, this is how the MongoDB Node driver does this "by default". So the behavior of adding in the "implicit" $set is being done by mongoose, unless you tell it not to.
NOTE The true way to "replace" would actually be to use replaceOne, either as the API method of replaceOne() or through bulkWrite(). The overwrite is a legacy of how mongoose wants to apply $set as described and demonstrated above, however the MongoDB official API introduces replaceOne as a "special" king of update() operation which does not allow the usage of atomic operators like $set within the statement and will error if you try.
This is much clearer semantically since replace reads very clearly as to what the method is actually used for. Within standard API calls to the update() variants of course still allow you to omit the atomic operators and will just replace content anyway. But warnings should be expected.
You can pass upsert option, and it will replace document:
var collection = db.collection('test');
collection.findOneAndUpdate(
{'_id': 'some_mongodb_id'},
{name: 'Dan smith Only'},
{upsert: true},
function (err, doc) {
console.log(doc);
}
);
But the problem here - is that doc in callback is found document but not updated.
Hence you need perform something like this:
var collection = db.collection('test');
collection.update(
{'_id': 'some_mongodb_id'},
{name: 'Dan smith Only'},
{upsert: true},
function (err, doc) {
collection.findOne({'_id': 'some_mongodb_id'}, function (err, doc) {
console.log(doc);
});
}
);

Resources