如果我们使用mongoose创建多个文档,mongoose时间戳功能创建的createdAt是否唯一?
mongoose
createdAt
ajsxfq5m1#
否,这些字段在数据库级别没有强制执行唯一性。从使用的Angular 来看,我们可以查看mongoose源代码,以了解这些字段是如何创建的:
const createdAt = handleTimestampOption(timestamps, 'createdAt'); const updatedAt = handleTimestampOption(timestamps, 'updatedAt'); const currentTime = timestamps != null && timestamps.hasOwnProperty('currentTime') ? timestamps.currentTime : null; const schemaAdditions = {}; schema.$timestamps = { createdAt: createdAt, updatedAt: updatedAt }; if (updatedAt && !schema.paths[updatedAt]) { schemaAdditions[updatedAt] = Date; }
因此,我们不需要按照整个流程来理解这是在内存中创建的。基本上,当你创建一个新文档时,如果你启用了时间戳,模式会创建这两个默认字段。这意味着从实际使用的Angular 来看,如果你只运行一个应用程序,你不太可能得到两个相同的时间戳。如果您运行多个更新的多个进程,则这种情况更有可能发生。
1条答案
按热度按时间ajsxfq5m1#
否,这些字段在数据库级别没有强制执行唯一性。
从使用的Angular 来看,我们可以查看
mongoose
源代码,以了解这些字段是如何创建的:因此,我们不需要按照整个流程来理解这是在内存中创建的。基本上,当你创建一个新文档时,如果你启用了时间戳,模式会创建这两个默认字段。这意味着从实际使用的Angular 来看,如果你只运行一个应用程序,你不太可能得到两个相同的时间戳。
如果您运行多个更新的多个进程,则这种情况更有可能发生。