Questions tagged [findoneandupdate]

45 questions
4
votes
1 answer

Mongoose findOneAndUpdate with undefined set as null

I have problem understanding findOneAndUpdate with undefined and null. when creating an object: const user = new User({ _id: 1, name: "foo", address: undefined }); user.save() It's creating a new document with only name and no address: { …
ET-CS
  • 6,334
  • 5
  • 43
  • 73
2
votes
1 answer

Node.js findOneAndUpdate function returns null when trying to update products in an ecommerce store with Express

I am trying to create a product update function for an ecommerce store, but the Product.findOneAndUpdate() keeps returning null. const updateProduct = await Product.findOneAndUpdate({ id }, req.body, { new: true, }); here's my route: const…
Chuquemeka
  • 33
  • 5
1
vote
2 answers

Mongoose findOneAndUpdate is not updating database

I have a database that has a structure to this. I've updated the values clearly, but the idea remains. I'm trying to essentially find the refresh token and update it with a new token. Here is the database structure sample { "_id":…
MPiland
  • 25
  • 6
1
vote
0 answers

Mongoose findone returns wrong user when the field passed in, is not defined in schema. How can I handle this case?

I noticed a case where (accidentally) the value passed into Model.findOne was a key that is not defined in schema. I expected an error or undefined but what is happening is that for exampe when I search for a user, if the value i pass in is…
user6377312
1
vote
2 answers

Appending objects to array field using $push and $each returns null (Mongoose)

Background: I have an array (A) of new objects that need to be added to an array field in Mongoose. if successful it should print to screen the newly updated object but it prints null. I checked the database and confirmed it also does not update at…
Luckyfield
  • 73
  • 2
  • 8
1
vote
1 answer

findOneAndUpdate a nested object inside a nested array

I want to update a nested object inside another nested object in MongoDb that looks like this : [ { _id: ObjectId("637c648fb8fcfb2bc3071bb9"), consultant_name: "Sam letterman", consultantUsername: "sam", consultant_Password:…
Vaodi
  • 37
  • 6
1
vote
0 answers

Mongoose findOneAndUpdate is not updating the DB

I am learning the Mongoose, GraphQL, Node.js stack. In my application, each Book object has a field similarBookIds to store books similar to that book. I have written a updateBook mutation that takes bookId to which similar books need to be added…
user3903448
  • 320
  • 1
  • 8
1
vote
0 answers

Updating Mongoose Subdocument Array By ID

I am using GraphQL and Mongoose, and there is a subdocument within the document "Events" that will have an array of "tasks". I want users to be able to perform standard CRUD operations on these tasks and events, and I have been able to implement…
1
vote
0 answers

Mongoose findOneandUpdate() $push dynamic update key problem

let countryId = 6263135fd3456f312f76e8cb let cityKey = 'country.'+(0); await CountryModel.findOneAndUpdate( { _id: countryId}, { $push: {cityKey:[]}}, {new:true}); To understand , country Id is the key of this multidimensional array. the array…
1
vote
2 answers

mongoDb - How do I delete an object from a nested array

"ProductCategory": [ { "_id": "6246e0d09b16cf549256ed75", "name": "Arduino Boards", "desc": "this is for demo purpose", "date": "2022-03-31T18:30:00.000Z", "products": [ { …
1
vote
0 answers

MONGOOSE, "$" Operator does't work as expected // elemMatch

This is the command const filter = { searchWord: req.body.searchWord, result: { $elemMatch: { idMeal: req.body.mealData.idMeal } }, }; const update = { $set: { 'result.$.strMeal': req.body.mealData.strMeal } }; const returnData = {…
Lajos Bela
  • 49
  • 6
1
vote
2 answers

Mongoose Error when using findOneAndUpdate

I have received an error at my server console during updating my Note collection, the update is successful but I receive this error in my server console, felt something wrong. Thanks in advance app.put("/todos/:id", async(req,res) => { try { …
Yu Hien Ng
  • 45
  • 6
1
vote
1 answer

Mongoose - is it possible to overwrite findOneAndUpdate options 'new' flag as true in schema level?

Currently, I can add new: true flag in options in each and every findOneAndUpdate query to get updated document, but would like to know if there is an option to overwrite this on global level, so I dont need to add this flag in every…
Saravanan S
  • 1,021
  • 1
  • 10
  • 24
1
vote
0 answers

findOneAndUpdate updating incorrect document

I'm trying to update a document with findOneAndUpdate(). However, a document with different id is getting updated. Code is as below: async function updateDocument(id){ const result = await Collection1_class.findOneAndUpdate(id,{ $set : {…
nandhini
  • 11
  • 2
1
vote
1 answer

Dynamic key without Object solution - MongoDB findOneAndUpdate call

I was able to successfully use a { key: value } structure for the ... const findOneAndUpdateUpdate = { $set: ratingsHistoryObject, $inc: ratingIncrementObject } ... part in the below code. However, for the .. const…
beyondtdr
  • 411
  • 6
  • 17
1
2 3