MongoDB: Upserting and Sub documents

后端 未结 1 510
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 15:49

Let\'s assume the following schema:

{
  \'_id\' : \'star_wars\',
  \'count\' : 1234,
  \'spellings\' : [ 
    { spelling: \'Star wars\', total: 10}, 
    { spell         


        
相关标签:
1条回答
  • 2021-01-25 16:05

    You could change your schema a little, though. If your documents looked like this:

    {
      '_id' : 'star_wars',
      'count' : 1234,
      'spellings' :  
        { 
            'Star wars': 10, 
            'Star Wars': 15, 
            'sTaR WaRs': 5
        }
    }
    

    Your updates would become as simple as:

    db.movies.update({_id:"star_wars"},{$inc:{"spellings.Star Wars":1}},true)
    
    0 讨论(0)
提交回复
热议问题