LoginSignup
0
0

More than 1 year has passed since last update.

MongoEngine ListFieldサイズでフィルタリング

Posted at

私のプロジェクトでは、ListFieldを使用したモデル。 あなたはこのコードを見ることができます。

列情報を格納するためのこのcolumn_list変数。

class Courses(DynamicDocument):
    id = ObjectIdField(primary_key=True, default=lambda: bson.ObjectId())
    column_list = ListField(LazyReferenceField(Columns), default=lambda: [])
    title = StringField()
    thumbnail = StringField()
    about = StringField()

フィルタを介してすべての空のcolumn_listを検索したい場合。

このようなコードを使用できます。

Courses.objects.filter(column_list__0__exists=False)

0はcolumn_listのインデックスです。これはこのコードと同等です。

Courses.objects.filter(__raw__={'$where': 'this.column_list.length <= 0'})

これに基づいて、column_list__0はcolumn_list[0]と同等であると考えることができます。

したがって、0より大きいサイズをフィルタリングする場合は、これを使用できます。

Courses.objects.filter(column_list__0__exists=True)

インデックスは0から始まるため、0は1つのレコードがあることを意味します。

これで、ListFieldのサイズをフィルタリングできます。

読んでくれてありがとう。

0
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
0
0