Sithys
Sithys

Reputation: 3793

How to find the last item with a specific value set to true in mongoose

I have a Schema for a car and some activityReports for this car. Now i need to find the latest activityReport where noteAlert is set to true. So i thought about sth. like this

ActivityReport.find().where('noteAlert').equals(true).exec(function (err, activityReports) {
    activityReports.set({
        noteAlert: false
    })
    activityReports.save();
})

but this doesn't work.

What i need is a function that sets showAsWarning to false for any found object if any newer Object has it set to true in creation - i hope it is clear what i need and someone can help.

Here's a screenshot of the objects inside the database. As you can see, there are some with noteAlert set to true and now i'd like to set them all to false.

enter image description here

Upvotes: 2

Views: 54

Answers (1)

Ashh
Ashh

Reputation: 46451

You are making wrong query here the correct syntax for using update query is

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>
   }
)

you can find more here

So now you should try something like this

ActivityReport.update(
  { noteAlert: true },               //query
  { $set: { noteAlert: false}},      //update operator
  { new: true })                     // gives you new result
.exec(function (err, activityReports) {
   console.log(activityReports)
})

Upvotes: 2

Related Questions