在该文档终于附加的子图表中,为什么?

发布于 2025-01-25 17:15:56 字数 523 浏览 1 评论 0原文

使用此架构

mongoose.Schema({
    world: String,
    color: [{ name: String }]
});

给我一个文档,其中包含_id字段的子文档。

{ _id: 'a9ec8475bf0d285e10ca8d42'
  world: 'matrix', 
  color: [
    { name: 'blue',  _id: '4a8c0e12135fa32e13db9ce9' },
    { name: 'red',   _id: '4a8c0a62254cd32e13db4ad8' },
    { name: 'white', _id: '4a8c04e2687ea32e13db1da7' }
]

为什么在子图表的情况下,_id添加/附加的最后一次添加/附加?

是否有办法首先保留_id在文档中?

Using this schema

mongoose.Schema({
    world: String,
    color: [{ name: String }]
});

Gives me a document that has sub-documents containing _id fields.

{ _id: 'a9ec8475bf0d285e10ca8d42'
  world: 'matrix', 
  color: [
    { name: 'blue',  _id: '4a8c0e12135fa32e13db9ce9' },
    { name: 'red',   _id: '4a8c0a62254cd32e13db4ad8' },
    { name: 'white', _id: '4a8c04e2687ea32e13db1da7' }
]

Why is the _id added/appended last in the case of the subdocument?

Is there a way to keep _id first in the document?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2025-02-01 17:15:56

将颜色定义为单独的架构,然后在主架构中放置颜色type colorschema

var colorSchema= mongoose.Schema({
// your subschema content
}, { _id : false });

define color as a separate schema as below, then in the main schema put color of type colorSchema

var colorSchema= mongoose.Schema({
// your subschema content
}, { _id : false });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文