查找其子文档字段与给定标准与猫鼬部分匹配的文档

时间:2019-06-20 17:59:25

标签: javascript node.js mongodb mongoose subdocument

我已经使用nodeJS和mongoose / mongoDB来构建API。对于通用模型(Gmodel),请考虑以下模型:

{
   name: "teleni",
   type: "person",
   data: mixed
}

此文档中的数据字段是混合类型,因此可以是任何类型,但我打算在大多数情况下将其作为子文档/对象(取决于类型字段的值)。如果我想查找所有类型为person的文档,我将执行以下代码:

Gmodel.find({type: "person"}, callbackFunction)

现在考虑模型的另一个实例:

{
   name: "teleni",
   type: "person",
   data: {
      data_type: "event",
      date: "Jan 30 2019"
   }
}

我的问题是我想使用猫鼬在数据字段为“事件”的对象/文档中查找所有文档。我尝试了以下代码:

Gmodel.find({data: {data_type: "event"}}, callbackFunction)

但是,当我执行此代码时,什么都没有返回,因为find正在寻找数据字段正好为{data_type: "event"}的Documents。因此,如何使用find来检索其子文档(混合类型)部分符合条件的文档,类似于find在顶级字段上的工作方式。

1 个答案:

答案 0 :(得分:0)

您可以使用点语法Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing function ChangeDesc { $form = New-Object Windows.Forms.Form ... $script:result = $form.ShowDialog() $script:hostname = $HNInputBox.Text $script:description = $DescInputBox.Text } do { ChangeDesc } while ($script:result -eq [Windows.Forms.DialogResult]::Retry) $PC = Get-WmiObject Win32_OperatingSystem -Computer $script:hostname $PC.Description = $script:description $PC.Put() 在子文档中指定一个字段。这样,它将仅基于此字段进行匹配,而不是原始代码中的整个子文档。

data.data_type
相关问题