Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript - MongoDB query: How do I find an object that matches two fields (when updating another field)?

I am trying to update the 'expelled' field to true, when students' ages are 13, and 'strikes' count is 3 - in the school with an ID of 101.

To rephrase it, how do I update a value in an object that is found based on a query of two matching criteria within the object being updates. An illustration of the collection is below.

collection: [
  { schoolID: 101,
    students: [
      { 
        name: "Jim",
        age: 13,
        strikes: 3,
        expelled: false
      },
      { 
        name: "Zack",
        age: 13,
        strikes: 0,
        expelled: false
      },
      { 
        name: "Sarah",
        age: 11,
        strikes: 1,
        expelled: false
      }
    ]
  },
  { schoolID: 102,
    students: [...]
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...