将猫鼬数据填充到集合中(由trapi.io生成)

时间:2019-06-25 03:33:30

标签: mongodb mongoose strapi

我在trapi.io生成的mongodb中有3个集合,我想在项目中按猫鼬定制查询填充数据。但是我无法填充数据(如stradi result)。

类别集合:

{
     _id: "5d10a731c5077836540bebf0",
     posts: [
     "5d10a6fbc5077836540bebed",
     "5d10af45c5077836540bebf1"
     ],
     name: "Support",
     ids: "support",
     keywords: "support",
     des: "support des",
     createdAt: "2019-06-24T10:34:25.383Z",
     updatedAt: "2019-06-24T11:08:53.475Z",
     __v: 0,
     id: "5d10a731c5077836540bebf0"
}

帖子集:

{
    _id: "5d10af45c5077836540bebf1",
    ids: "test",
    title: "this is test",
    des: "test",
    keywords: "test",
    body: "testtesttesttesttest",
    createdAt: "2019-06-24T11:08:53.467Z",
    updatedAt: "2019-06-24T11:08:53.477Z",
    __v: 0,
    id: "5d10af45c5077836540bebf1"
}

FileUpload集合:

{
    _id: "5d10af45c5077836540bebf2",
    name: "THUMBNAIL.png",
    sha256: "0bP9PI3R_ygF07cLhg5U-syLeVCz4ZbBrwZZi2OtPL4",
    hash: "9f4e8bbd13b94f2baa3d26b335124717",
    ext: ".png",
    mime: "image/png",
    size: "297.33",
    url: "/uploads/9f4e8bbd13b94f2baa3d26b335124717.png",
    provider: "local",
    related: 
    [
        {
        _id: "5d10af45c5077836540bebf3",
        ref: "5d10af45c5077836540bebf1",
        kind: "Post",
        field: "pic"
        }
    ],
    createdAt: "2019-06-24T11:08:53.501Z",
    updatedAt: "2019-06-24T11:08:53.505Z",
    __v: 0,
    id: "5d10af45c5077836540bebf2"
}

我可以使用

填充类别模型上的“帖子”
Cat.find().populate({path: 'posts',model: 'Post'})

但是我无法在Post模型中填充“类别”和“图片”。

我如何像stradi的结果那样填充“类别”和“图片”:

{
    _id: "5d10af45c5077836540bebf1",
    ids: "test",
    title: "this is test",
    des: "test",
    keywords: "test",
    body: "testtesttesttesttest",
    createdAt: "2019-06-24T11:08:53.467Z",
    updatedAt: "2019-06-24T11:08:53.477Z",
    __v: 0,
    id: "5d10af45c5077836540bebf1",
    pic: {
        _id: "5d10af45c5077836540bebf2",
        name: "THUMBNAIL.png",
        sha256: "0bP9PI3R_ygF07cLhg5U-syLeVCz4ZbBrwZZi2OtPL4",
        hash: "9f4e8bbd13b94f2baa3d26b335124717",
        ext: ".png",
        mime: "image/png",
        size: "297.33",
        url: "/uploads/9f4e8bbd13b94f2baa3d26b335124717.png",
        provider: "local",
        related: [
            "5d10af45c5077836540bebf1"
        ],
        createdAt: "2019-06-24T11:08:53.501Z",
        updatedAt: "2019-06-24T11:08:53.505Z",
        __v: 0,
        id: "5d10af45c5077836540bebf2"
    },
    categories: 
    [
        {
            posts: [
                "5d10a6fbc5077836540bebed",
                "5d10af45c5077836540bebf1"
            ],
            _id: "5d10a731c5077836540bebf0",
            name: "Support",
            ids: "support",
            keywords: "support",
            des: "support des",
            createdAt: "2019-06-24T10:34:25.383Z",
            updatedAt: "2019-06-24T11:08:53.475Z",
            __v: 0,
            id: "5d10a731c5077836540bebf0"
        }
    ]
}

1 个答案:

答案 0 :(得分:-1)

您必须在帖子内创建另一个收藏集。就像您有一个独立的馆藏一样,您就可以填充它。例如:

帖子:{正文:,标题:,图片:{图片:{名称:等}}}

这可以是collection内部的collection。上面的代码不正确,您必须提及自己的字段。