有没有一种方法可以创建一个包含许多mongoDB objectiID的数组属性

时间:2020-01-31 16:15:36

标签: arrays mongodb model objectid loopback4

我使用Loopback4。 当我想在模型中添加mongoDb ObjectId属性时,我可以这样做:

    @property({
        type: 'string',
        mongodb: {dataType: 'ObjectID'},
    })
    organizationId?: string;

现在我想创建一个带有MongoDB ObjectId属性的数组,所以我尝试这样做:

    @property({
        type: 'array',
        itemType: 'string',
        mongodb: {dataType: 'ObjectID'},
    })
    tagsId?: string[];

但似乎所有数组都在mongoDb中转换为一个ObjectID。

我想做的就是简单地获取内部包含许多ObjectId的数组。我尽我所能尝试了所有事情:这还不够。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案: 第1步 : 创建一个只有一个ID的模型。 第2步 : 用您的新模型制作一个阵列

第1步: 在您的未来模型中(以我为例:tagReference):

@model()
export class TagReference extends Entity {
    @property({
        type: 'string',
        mongodb: {dataType: 'ObjectID'},
    })
    id?: string;

    constructor(data?: Partial<TagReference>) {
        super(data);
    }
}

步骤2: 您想要数组的位置:

import {TagReference} from './tag-reference.model';

@model()
export class Resource extends BaseEntity {

    // ...

    @property({
        type: 'array',
        itemType: TagReference,
    })
    tagIds?: string[];

   // ...
}
相关问题