1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

$pullの条件にidを指定する場合の注意

1
Posted at

Mongoidで少し複雑な更新処理を書こうとして、Mopedを直接叩く場合の注意です。
update()内で、$pullでの削除条件にidを指定する場合は、Moped::BSON::ObjectId型で渡す必要があります。

Project.collection.find({ '_id' => project_id }).update({
  "$pull"=>{"tasks"=>{ '_id' => Moped::BSON::ObjectId(task_id) } }
})

困ったことに、find()内では文字列で渡しても動きます。
$pullでも同じでいいだろうと思うとハマるので気をつけましょう。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?