首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套文档上的猫鼬模式集时间戳

嵌套文档上的猫鼬模式集时间戳
EN

Stack Overflow用户
提问于 2020-01-28 17:17:37
回答 1查看 3.3K关注 0票数 3

我有一个模式,如下所示:

代码语言:javascript
复制
{
  "_id": "5073c76a23ce3abf0f000001",
  "asker": {
    "userId": "fooId",
    "firstName": "foo",
    "lastName": "bar",
    "points": "10",
    "aboutMe": "Something about me"
  },
  "isBounty": false,
  "tags": ["mongodb", "nosql", "mongodb-query"],
  "title": "Is there merit to adding flat properties additional to a duplicate nested",
  "descriptionMd": "Question description",
  "offeredPoints": 10,
  "slug": "is-there-merit-to-adding-flat-properties-additional-to-a-duplicate-nested",
  "biddings": [{
    "biddingId": "_biddingId",
    "respondent": {
      "userId": "fooId",
      "firstName": "foo",
      "lastName": "bar",
      "points": "10",
      "aboutMe": "Something about me"
    },
    "biddingPoints": 10,
    "createdAt": "2019-03-21T08:00:00",
    "lastUpdatedAt": "2019-03-21T08:00:00"
  }],
  "acceptedBidding": "_biddingId",
  "answers": [
    {
      "respondent": {
        "address": {
          "userId": "fooId",
          "firstName": "foo",
          "lastName": "bar",
          "points": "10",
          "aboutMe": "Something about me"
        }
      },
      "answerTextMd": "Answer 1",
      "reviewRequested": true,
      "reviewer": {
        "userId": "fooId",
        "firstName": "foo",
        "lastName": "bar",
        "points": "10",
        "aboutMe": "Something about me"
      },
      "reviewStatus": "ANSWER_ACCEPTED",
      "createdAt": "2019-03-21T08:00:00",
      "lastUpdatedAt": "2019-03-21T08:00:00"
    }
  ],
  "createdAt": "2019-03-21T08:00:00",
  "lastUpdatedAt": "2019-03-21T08:00:00"
}

此模式用于问答论坛,我更喜欢将所有数据嵌入到问题文档中。

所需经费如下:

在问题文档上设置创建和更新的

  • 时间戳,
  • 时间戳应该放在嵌套的投标和答案上,以及

我知道在问题文档上设置时间戳的默认方法:

代码语言:javascript
复制
const mySchema = new mongoose.Schema( {name: String}, {timestamps: true} );

如何在更新时将时间戳动态放置在嵌套文档上?

是否有一种明智的做法,还是我应该自己将字段放在那里,并手动更新它们?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-28 18:04:36

还可以对内部架构应用mongoose架构时间戳选项。

例如,在下面的模式中,我将timestamps: true选项应用于内部投标模式。

代码语言:javascript
复制
const mongoose = require("mongoose");

const forumSchema = new mongoose.Schema(
  {
    title: { type: String, required: true },
    biddings: [
      {
        type: new mongoose.Schema(
          {
            biddingId: String,
            biddingPoints: Number
          },
          { timestamps: true }
        )
      }
    ]
  },
  { timestamps: true }
);

const Forum = mongoose.model("Forum", forumSchema);

module.exports = Forum;

现在让我们来测试一下:

我用以下代码创建了一个论坛文档:

代码语言:javascript
复制
const Forum = require("../models/forum");

router.post("/forums", async (req, res) => {
  const result = await Forum.create(req.body);
  res.send(result);
});

请求机构:

代码语言:javascript
复制
{
    "title": "Title 1",
    "biddings": [
        {
            "biddingId": "bidding1",
            "biddingPoints": 10
        },
        {
            "biddingId": "bidding2",
            "biddingPoints": 30
        }
    ]
}

响应:(正如您所看到的,时间戳都应用于父文档和子文档)

代码语言:javascript
复制
{
    "_id": "5e3073b3a2890b03b029e92c",
    "title": "Title 1",
    "biddings": [
        {
            "_id": "5e3073b3a2890b03b029e92e",
            "biddingId": "bidding1",
            "biddingPoints": 10,
            "createdAt": "2020-01-28T17:47:31.376Z",
            "updatedAt": "2020-01-28T17:47:31.376Z"
        },
        {
            "_id": "5e3073b3a2890b03b029e92d",
            "biddingId": "bidding2",
            "biddingPoints": 30,
            "createdAt": "2020-01-28T17:47:31.376Z",
            "updatedAt": "2020-01-28T17:47:31.376Z"
        }
    ],
    "createdAt": "2020-01-28T17:47:31.376Z",
    "updatedAt": "2020-01-28T17:47:31.376Z",
    "__v": 0
}

现在让我们用_id:5e3073b3a2890b03b029e92e更新投标点

代码语言:javascript
复制
router.put("/forums/:forumId/biddings/:biddingId",
  async (req, res) => {
    let points = req.body.points;

    try {
      let result = await Forum.findByIdAndUpdate(
        req.params.forumId,
        {
          $set: {
            "biddings.$[inner].biddingPoints": points
          }
        },
        {
          arrayFilters: [{ "inner._id": req.params.biddingId }],
          new: true
        }
      );

      if (!result) return res.status(404);

      res.send(result);
    } catch (err) {
      console.log(err);
      res.status(500).send("Something went wrong");
    }
  }
);

url将如下所示:http://.../forums/5e3073b3a2890b03b029e92c/biddings/5e3073b3a2890b03b029e92e

要求:(这意味着我想用_id:5e3073b3a2890b03b029e92e更新到50点的投标

代码语言:javascript
复制
{
    "points": 50
}

响应:(正如您看到的,更新后的投标的updatedAt字段值从2020-01-28T17:47:31.376Z自动更改为2020-01-28T17:50:03.855Z )

代码语言:javascript
复制
{
    "_id": "5e3073b3a2890b03b029e92c",
    "title": "Title 1",
    "biddings": [
        {
            "_id": "5e3073b3a2890b03b029e92e",
            "biddingId": "bidding1",
            "biddingPoints": 50,
            "createdAt": "2020-01-28T17:47:31.376Z",
            "updatedAt": "2020-01-28T17:50:03.855Z"   ==> UPDATED
        },
        {
            "_id": "5e3073b3a2890b03b029e92d",
            "biddingId": "bidding2",
            "biddingPoints": 30,
            "createdAt": "2020-01-28T17:47:31.376Z",
            "updatedAt": "2020-01-28T17:47:31.376Z"
        }
    ],
    "createdAt": "2020-01-28T17:47:31.376Z",
    "updatedAt": "2020-01-28T17:50:03.855Z",
    "__v": 0
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59953941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档