Mongo 中存储的数组无法与具有相同长度和值的本机 JavaScript 数组进行深度断言比较
我在 mongo 中有一个使用 mongoose ORM 定义的字段,如下所示:
state: {type: [Number], required: true }
如果我使用 mongo 控制台查看示例文档,状态看起来像
state: [ 1, 1, 1 ]
到目前为止,一切都很好。但奇怪的是,对于同一文档,以下断言失败了:
assert.deepEqual state, [ 1, 1, 1 ]
无法弄清楚这是否是我在 JS 中的对象比较中遗漏的内容,或者与 mongo 返回状态数组的方式有关。
I have a field in mongo defined with the mongoose ORM like so:
state: {type: [Number], required: true }
If I take a peek at a sample document with the mongo console, state looks like
state: [ 1, 1, 1 ]
So far, so good. But strange enough for that same document the following assert fails:
assert.deepEqual state, [ 1, 1, 1 ]
Can't figure out if this is something I'm missing with object comparisons in JS or something to do with the way mongo is returning the state array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MongoDB 有一个 bug,其中应该枚举不可枚举的属性:
例如,一个值为: 的数组:
根据
Object.keys()
具有以下键:注意 Mongo 现在使用 V8,它支持 ES5,多年来它一直能够通过 Object.defineProperty() 创建不可枚举属性。
正如另一张海报提到的:
MongoDB has a bug where properties that should be non-enumerable are enumerated:
Eg, an array whose values are:
has the following keys, according to
Object.keys()
:Note Mongo now uses V8, which supports ES5, which has had the ability to create non-enumerable properties via Object.defineProperty() for many, many years.
As the other poster mentions: