简体   繁体   中英

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. 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? 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. 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
    }
]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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