猫鼬中的嵌套数组 [英] Nested arrays in Mongoose

查看:58
本文介绍了猫鼬中的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的集合中,文档看起来像这样:

In the collection I'm working on, a document looks like this:

{
    name: 'Myname',
    other: 'other',
    stuff: [
        ['something', 12, 4, 'somethingelse'],
        ['morestuff', 2, 4, 8],
        ['finally', 12, 'again', 58],

    ]
}

我写了这个Mongoose模式来访问它:

I wrote this Mongoose schema to access it:

var MyDocSchema = new Schema({
    name: String,
    other: String,
    stuff: [],
});

查询文档时,一切正常,控制台中显示的输出正确.但是,当我尝试执行console.log(myDoc.stuff)时,得到了以下信息:

When I query a doc, everything works well, the output shown in the console is right. But when, I try to do console.log(myDoc.stuff), I got the following:

['something', 12, 4, 'somethingelse', 'morestuff', 2, 4, 8, 'finally', 12, 'again', 58]

代替

[
    ['something', 12, 4, 'somethingelse'],
    ['morestuff', 2, 4, 8],
    ['finally', 12, 'again', 58],

]

我做错了什么?谢谢您的帮助!

What am I doing wrong? Thank you for your help!!

推荐答案

免责声明:该回复的日期很早,2012年!可能不是最准确.

来自Mongoose文档.

http://mongoosejs.com/docs/schematypes.html :滚动向下到数组"部分:

Disclaimer: This response is pretty dated, 2012! It might not be the most accurate.

From the Mongoose documentation.

http://mongoosejs.com/docs/schematypes.html: Scroll down to the Array section:

注意:指定一个空数组等效于[Mixed].这 以下所有创建Mixed的数组.

Note: specifying an empty array is equivalent to [Mixed]. The following all create arrays of Mixed.

"Array"部分正上方的"Mixed"部分详细说明了什么.

Details on what that means is in the Mixed section right above the Array section.

为嵌入式文档定义架构:

Define a schema for the embedded documents:

var Stuff = new Schema({
  name: String,
  value1: Number,
  ...
});

使用它代替空数组[]:

var MyDocSchema = new Schema({
  name: String,
  other: String,
  stuff: [Stuff],
});

这篇关于猫鼬中的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆