title: mongoose filter by multiple conditions and execute to update data
body: <p>I am wondering what would be the best approach to make schema functions using mongoose. I have never used this so the way I think is somewhat limited, same goes for looking for docs, without knowing what's available, is not very efficient.</p>
  <p>Through docs I found that either using findOneAndUpdate might solve the problem; but there are some constraints.</p>
  <p>Here is the code I am planning to run:</p>
  <p>models/Bookmark.js</p>
  <pre><code>const mongoose = require('mongoose')
  
  const bookmarkItemSchema = new mongoose.Schema({
      restaurantId: String,
      cachedAttr: {
          name: String,
          latitude: Number,
          longitude: Number,
      },
  })
  
  const bookmarkListSchema = new mongoose.Schema({
      listName: String,
      items: [bookmarkItemSchema],
  })
  
  const bookmarkSchema = new mongoose.Schema({
      userId: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'User',
      },
      lists: [bookmarkListSchema],
  })
  
  // const add = (lists, userId) =&gt; {
  //     let bookmark = Bookmark.findOne({userId})
  //     bookmark.lists.listName === lists.listName //current, new
  //         ? bookmark.lists.items.push(lists.items)
  //         : bookmark.lists.push(lists)
  //     return bookmark
  // }
  
  mongoose.model('Bookmark', bookmarkSchema)
  </code></pre>
  <p>Routes/bookmark.js</p>
  <pre><code>router.post('/bookmarks', async (req, res) =&gt; {
      const {lists} = req.body
      console.log(lists)
      if (!lists) {
          return res.status(422).send({error: 'You must provide lists'})
      }
  
      let bookmark = Bookmark.findOne({&quot;userId&quot;: req.user._id})
      if (bookmark.lists.listName === lists.listName){
          let item = lists.items
          bookmark.lists.items.push(item)
          await bookmark.save()
          res.send(bookmark)
      }
  
      try {
          // const bookmark = Bookmark.add(lists, req.user._id, obj)
          // await bookmark.save()
          // res.send(bookmark)
          let bookmark = Bookmark.findOne({&quot;userId&quot;: req.user._id})
          if (bookmark.lists.listName === lists.listName){ // THIS IS UNDEFINED. How to get this object?
              let item = lists.items
              bookmark.lists.items.push(item)
              await bookmark.save()
              res.send(bookmark)
          }
      } catch (e) {
          res.status(422).send({error: e.message})
      }
  })
  </code></pre>
  <p>The req.body looks like this:</p>
  <pre><code>{
    &quot;lists&quot;: {
      &quot;listName&quot;: &quot;My Saved List&quot;,
      &quot;items&quot;: {
        &quot;restaurantId&quot;: &quot;abcdefg&quot;,
        &quot;cachedAttr&quot;: {
          &quot;name&quot;: &quot;abcdefg&quot;,
          &quot;latitude&quot;: 200,
          &quot;longitude&quot;: 200
        }
      }
    }
  }
  </code></pre>
  <p>Basically what I commented out in the models/Bookmark.js file is what I would really like to do.</p>
  <p>If the userId's list name already exists, then I would like to just add an item to the list.
  Otherwise, I would like to add a new list to the object.</p>
  <p>What is the best approach for doing this? Is there a straight forward mongoose api that I could use for this problem? or do I need to make two separated function that would handle each case and make that as schema methods and handle it in the routes file?</p>