首页 > 解决方案 > 使用 $pull 和 Deployd 方法

问题描述

我有一个 Deployd API,它公开了一个像这样的 json 结构:

[
  {id: "1"
   username: "john",
   password: " ..... ",
   email: "example@gmail.com",
   coins: 60,
   badges: [ ],
   courses:[
       { id: "123456",
         title: "Animals",
         grades_per_module: [ [30], [28, 26], [25, 24]]
         ..... 
       },
       { id: "112233",
         title: "Food",
         grades_per_module: [ [20, 25, 27], [22]]
         ..... 
       }
    ]
  }, 

  {id: "2"
   username: "mark",
   password: " ..... ",
   email: "ex@gmail.com",
   coins: 40,
   badges: [ ],
   courses:[
       { id: "123456",
         title: "Animals",
         grades_per_module: [ [27], [21, 30], [30, 30]]
         ..... 
       }
    ]
  }
] 

然后我需要使用角码删除用户“john”的 id="112233" 的兴趣课程。

所以我使用这段代码,但它不起作用:

this.http.put('http://localhost:2403/users/1',
              { "courses": { $pull: { "id": 112233 } }
             }).subscribe( ..... )

已部署的 API 向我返回了一个肯定的消息,但课程并没有真正从用户对象中删除。谁能帮我?

标签: mongodbdeployd

解决方案


推荐阅读