如何在猫鼬中使用多个值对字段应用过滤器
how to apply filter on field using multiple values in mongoose
我有一个模型包含 state
类型的枚举并且只接受三个值
active
disabled
deleted
目前正在过滤以通过
获取所有活动的
const query = {state : "active" ... other filters here}
const posts = await post.find(query)
但我有一个案例,我需要获得同时说明 active
和 disabled
的帖子,我在 mongoose 文档中搜索了一些我可以使用的东西,但目前我什么也没找到我想要一些干净的东西来实现这个代码
const query1 = {state : "active" ... other filters here}
const posts1 = await post.find(query1)
const query2 = {state : "disabled" ... other filters here}
const posts2 = await post.find(query2)
const posts = {...posts1, ...posts2}
const query = { state: { $in: [ "active", "disabled"] } }
const posts = await post.find(query)
这将生成状态为 active
或 disabled
的帖子列表。
The $in operator selects the documents where the value of a field
equals any value in the specified array. To specify an $in expression,
use the following prototype:
来自 MongoDB docs.
我有一个模型包含 state
类型的枚举并且只接受三个值
active
disabled
deleted
目前正在过滤以通过
获取所有活动的const query = {state : "active" ... other filters here}
const posts = await post.find(query)
但我有一个案例,我需要获得同时说明 active
和 disabled
的帖子,我在 mongoose 文档中搜索了一些我可以使用的东西,但目前我什么也没找到我想要一些干净的东西来实现这个代码
const query1 = {state : "active" ... other filters here}
const posts1 = await post.find(query1)
const query2 = {state : "disabled" ... other filters here}
const posts2 = await post.find(query2)
const posts = {...posts1, ...posts2}
const query = { state: { $in: [ "active", "disabled"] } }
const posts = await post.find(query)
这将生成状态为 active
或 disabled
的帖子列表。
The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:
来自 MongoDB docs.