简体   繁体   English

自定义对象数组MongoDB模式

[英]Array of Custom Objects MongoDB Schema

I need to create an array of objects with two fields (no methods needed) in mongoDB so that the document has a field that is an array of the these objects with two fields. 我需要在mongoDB中创建具有两个字段(无需方法)的对象数组,以便文档具有一个字段,该字段是这些对象与两个字段的数组。 I have no idea what the schema syntax should be however. 我不知道架构语法应该是什么。 How would I go about this? 我将如何处理?

Also: Can I just declare an document with an array and fill it in with javascript objects created at run time? 另外:我可以只声明一个带有数组的文档,并用在运行时创建的javascript对象填充它吗? Or should I declare the custom object in the Schema for the array holding document? 还是应该在架构中为数组数组声明自定义对象?

Thanks so much! 非常感谢!

The following is an example creating an object with an array of object, each with 2 fields. 下面是一个使用对象数组创建对象的示例,每个对象都有2个字段。 Is this what you are looking for? 这是你想要的?

$ mongo
MongoDB shell version: 2.6.3
connecting to: test
> 
> 
> db.test.insert({myArray: [{a: 1, b: 2}, {a: 3, b:4}]})
WriteResult({ "nInserted" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("53adc9301e7d2620fc75f8c7"),
"myArray" : [
    {
        "a" : 1,
        "b" : 2
    },
    {
        "a" : 3,
        "b" : 4
    }
]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM