LoginSignup
3
0

More than 5 years have passed since last update.

mongodb/mongoid:ネストしたフィールドを検索する方法

Posted at

ドット記法で取れる

こんなデータを

{
  "name": "taro",
  "address": {
    "number": 1234,
    "building": "tokyo_tower"
  }
}

こう取る

User.where("address.building" => "tokyo_tower").as_json

補足

whereはトップレベルのみ検索できる(ネストされたフィールドは検索できない)と書かれた記事もあったのですが、ドット記法で問題無く取れました。古いバージョンなのかどうかはよくわかりません

参考

mongoDB - Query on Embedded/Nested Documents

バージョン

  • mongoid (6.0.2)
  • MongoDB shell version: 3.2.14
3
0
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
3
0