import { ObjectID } from 'bson';
让事情更加混乱的是mongoose文档和代码。mongoose也称之为ObjectId http://mongoosejs.com/docs/api.html#types-objectid-js,但当我阅读代码时,我看到了
// mongodb.ObjectID does not allow mongoose.Types.ObjectId(id). This is
// commonly used in mongoose and is found in an example in the docs:
// http://mongoosejs.com/docs/api.html#aggregate_Aggregate
// constructor exposes static methods of mongodb.ObjectID and ObjectId(id)
type ObjectIdConstructor = typeof mongodb.ObjectID & {
(s?: string | number): mongodb.ObjectID;
}
那么,ObjectID、ObjectId和mongoose ObjectId之间到底有什么区别?
我发现还有另一个Stack Overflow上讨论这个问题的帖子BSON::ObjectId vs Mongo::ObjectID。
不过那里的链接已经失效了,而且也没有提到mongoose。所以我希望我的问题不会被标记为重复。
ObjectID
的“字符串”值。重点是Schema.Types.ObjectId
对于定义的 mongoose 模式具有特殊含义。但是底层数据(实际实现)其实是一样的。 - Neil Lunn