创建新文档时,猫鼬数字ID递增

时间:2020-04-22 02:57:15

标签: mongodb mongoose mongoose-schema

我有一个mongo模型,

objectid

我将给用户_id作为数字ID,而不是给用户template const biggestTemplateRes = await db.templates .findOne() .sort({ template: -1 }); if (biggestTemplateRes) { biggestTemplate = biggestTemplateRes.toObject().template; } ++biggestTemplate; console.log({ biggestTemplate }); return db.templates.create({ creator, created, updated, template: biggestTemplate, ...attrs }); 带来麻烦且难以记住的数字,例如:1、2, 3、4 ...

问题在于,如果我采用这种方法,我必须:

          <h1 className="ui centered">Select Engagement Details</h1>
          <Form.Group widths="equal">
            <Form.Dropdown
              fluid
              selection
              placeholder="Client Name"
              onChange={(e, { value }) => {
                this.props.handleDropdown({
                  input: "clientName",
                  value: value,
                  optionType: "clientOptions",
                  options: this.state.clients
                });
              }}
              defaultValue={values.clientName}
              options={this.state.clients}
            />

            <Form.Dropdown
              fluid
              selection
              placeholder="Engagement Name"
              onChange={(e, { value }) => {
                this.props.handleDropdown({
                  value: value,
                  input: "engagementName",
                  optionType: "engagementNameOptions",
                  options: this.state.engagements
                });
              }}
              defaultValue={values.engagementName}
              options={this.state.engagements}
            />
          </Form.Group>
          <Button disabled={!isEnabled} onClick={this.saveAndContinue}>
            {" "}
            Continue
          </Button>

          {/* starts new engagement area */}
          <hr style={{marginTop: '30px'}}/>
          <h2 className="ui centered"> New Engagement </h2>

          <Form.Group controlId="newEngagementGroup" widths="equal">
            {/* type is not required. Type is useful for common types like "email" or "password" */}
            <Form.Input 
                  type="clientName" 
                  placeholder="Enter Client Name"
                  ref="newClient"
                  onChange={(e, { value }) => {
                    this.props.handleDropdown({
                      input: "newEngagementClientName",
                      value: value
                    })
                  }}
                  defaultValue={values.newClientName}
            />
            <Form.Input 
                  type="engagementType" 
                  placeholder="Enter Engagement Type"
                  ref="newEngagementType"
                  onChange={(e, { value }) => {
                    this.props.handleDropdown({
                      input: "newEngagementType",
                      value: value
                    })
                  }}
                  defaultValue={values.newEngagementType} />
          </Form.Group>``` 

搜索最大的模板,然后创建一个更大的模板...

  1. 通过再添加一个查询来降低性能。
  2. 如果删除了最大的模板,则可能会有问题,因此新创建的模板将具有与先前删除的模板相同的模板编号。

上面的第二个问题可以用一个额外的集合/模型来解决,我可以使用它来维护序列,但是并不能真正解决问题1 ...

创建新文档时,是否可以让mongo知道我希望模板成为具有增量的数字ID字段?

1 个答案:

答案 0 :(得分:0)

ObjectId解决了您正在使用id生成描述的确切问题。 ObjectId很可能是全局唯一的,并且是在客户端上生成的,而无需与数据库(或任何集中授权)进行对话。

如果要使用顺序自然数作为ID,则必须有一些数据存储来跟踪使用率最高的ID,应用程序需要与其进行交互(即执行往返)以找出要使用的下一个ID。

您可以将MongoDB用作数据存储,但是仍然需要执行往返操作以获取下一个要使用的ID。

MongoDB之所以没有像许多关系数据库那样立即提供此功能的原因是因为分片-使用分片时,应用程序正在与所有服务器的子集进行交互,因此没有中央授权可以告诉您使用了哪些ID。