MongoDB insert document “or” increment field if exists in array

后端 未结 2 1811
执笔经年
执笔经年 2021-02-20 14:55

What I try to do is fairly simple, I have an array inside a document ;

\"tags\": [ 
    {
        \"t\" : \"architecture\",
        \"n\" : 12
          


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-20 15:16

    I don't believe this is possible to do in a single command.

    MongoDB doesn't allow a $set (or $setOnInsert) and $inc to affect the same field in a single command.

    You'll have to do one update command to attempt to $inc the field, and if that doesn't change any documents (n = 0), do the update to $set the field to it's default value.

提交回复
热议问题