将投影设置为数组的字段不起作用。撇号CMS
Set projection to a field that is an array is not working. ApostropheCMS
我正在尝试设置投影以便从查询中检索较少的数据,但是当我尝试在投影中放入一个 array
字段时出现问题
这是我的piece schema
module.exports = {
name: "attendee",
extend: "apostrophe-pieces",
alias: "attendees",
label: "Attendee",
pluralLabel: "Attendees",
addFields: [
{
name: "_congress",
label: "Congress this attendee has sign up for",
type: "joinByOne",
withType: "congress",
filters: {
projection: {
_id: 1,
title: 1
}
}
},
{
name: "registrationDate",
label: "Registration Date",
type: "date"
},
{
name: "registrationTime",
label: "Registration Time",
def: null,
type: "time"
},
{
name: "fields",
label: "Fields",
type: "array",
titleField: "Array Label",
schema: [
{
name: "name",
type: "string",
label: "String"
},
{
name: "value",
type: "string",
label: "Value"
}
]
}
],
我的查询是这样的:
return self.find(req, { congressId }).projection({ registrationDate: 1, registrationTime: 1, fields: 1 }).toArray();
当我输入 fields: 1
时出现异常
否则 returns 数组如预期:
我也在 mongo shell 中尝试了这个查询并且它有效:
> db.aposDocs.find({_id: "ck35z61p400047e9e5ktt6wu4"}).projection( {fields: 1})
{ "_id" : "ck35z61p400047e9e5ktt6wu4", "fields" : [ { "id" : "attende1Field1", "name" : "name", "value" : "Name1" }, { "id" : "attendee1Field1", "name" : "lastName", "value" : "Last Name1" }, { "id" : "attendee1Field1", "name" : "treatment", "value" : "Treatment1" }, { "id" : "attendee1Field1", "name" : "email", "value" : "email1@email.com" } ] }
也许我错过了什么?提前致谢
这似乎是由 MongoDB 错误引起的,至少在 2.x 版本的 MongoDB nodejs 驱动程序中是这样。特别是字段名称 fields
不起作用并生成您看到的错误:
Failed to parse: projection: 1. 'projection' field must be of BSON type object.
我将其重命名为 fields2
,一切都按预期进行。所以你的解决方案只是更改数组字段的名称。看看配置的 apostrophe-mongo-3-driver
模块是否仍然出现此问题会很有趣。
我正在尝试设置投影以便从查询中检索较少的数据,但是当我尝试在投影中放入一个 array
字段时出现问题
这是我的piece schema
module.exports = {
name: "attendee",
extend: "apostrophe-pieces",
alias: "attendees",
label: "Attendee",
pluralLabel: "Attendees",
addFields: [
{
name: "_congress",
label: "Congress this attendee has sign up for",
type: "joinByOne",
withType: "congress",
filters: {
projection: {
_id: 1,
title: 1
}
}
},
{
name: "registrationDate",
label: "Registration Date",
type: "date"
},
{
name: "registrationTime",
label: "Registration Time",
def: null,
type: "time"
},
{
name: "fields",
label: "Fields",
type: "array",
titleField: "Array Label",
schema: [
{
name: "name",
type: "string",
label: "String"
},
{
name: "value",
type: "string",
label: "Value"
}
]
}
],
我的查询是这样的:
return self.find(req, { congressId }).projection({ registrationDate: 1, registrationTime: 1, fields: 1 }).toArray();
当我输入 fields: 1
时出现异常
否则 returns 数组如预期:
我也在 mongo shell 中尝试了这个查询并且它有效:
> db.aposDocs.find({_id: "ck35z61p400047e9e5ktt6wu4"}).projection( {fields: 1})
{ "_id" : "ck35z61p400047e9e5ktt6wu4", "fields" : [ { "id" : "attende1Field1", "name" : "name", "value" : "Name1" }, { "id" : "attendee1Field1", "name" : "lastName", "value" : "Last Name1" }, { "id" : "attendee1Field1", "name" : "treatment", "value" : "Treatment1" }, { "id" : "attendee1Field1", "name" : "email", "value" : "email1@email.com" } ] }
也许我错过了什么?提前致谢
这似乎是由 MongoDB 错误引起的,至少在 2.x 版本的 MongoDB nodejs 驱动程序中是这样。特别是字段名称 fields
不起作用并生成您看到的错误:
Failed to parse: projection: 1. 'projection' field must be of BSON type object.
我将其重命名为 fields2
,一切都按预期进行。所以你的解决方案只是更改数组字段的名称。看看配置的 apostrophe-mongo-3-driver
模块是否仍然出现此问题会很有趣。