简体   繁体   中英

Not understanding mongoose schema array syntax

I'm having trouble wrapping my head around the below syntax in a mongoose schema.

tokens:[{
        token:{
            type: 'String',
            required: true
        }
    }]

Normally when I want to add objects to arrays I would just push the object. That includes if I want to push an object with an internal object to said array like the example below.

let obj =[]
obj.push({name: 'dirk', age: 24})
obj.push({name: 'chris', age:29, clothes: {shirt: 'black', pants: 'jeans'}, height: '6ft'})

So im confused in mongoose as to why I need this syntax [{}] to use an array?

Ok I'll try to explain this as best I can. In basic JavaScript, an array is just a bucket of "stuff" for lack of better words. What that means is, you could have something like this:

let myList = []
myList.push({name: "John Doe", age: 20})
myList.push({car: {make: "Honda", model: "Accord"}})
myList.push({menu_items: ["item 1", "item 2", "item 3"]})

And JavaScript won't really care right? As far as it's concerned, you haven't done anything wrong because you technically did the correct thing, put some object into a list that can hold whatever you want.

Mongoose, which is an ORM (check out the term if you haven't heard of it before), requires things be a little more strict. Remember that Mongoose is trying to map documents from the Mongo database to this "Object" that should be standard and readable from any point in the code the same what. So in the example in your question listed above:

tokens:[{
    token:{
        type: 'String',
        required: true
    }
}]

you are saying to Mongoose that it should expect tokens to contain a list of token objects that have a certain design to them. Now, when you want to add more token objects to the tokens list, you can do something like this:

token = { 
    // YOUR CORRECT TOKEN DATA
}

tokens.push(token);

If you tried to do something like this instead:

token = { 
    // RANDOM WRONG DATA
}

tokens.push(token);

Mongoose wouldn't take kindly to it because you are violating the constraints you told Mongoose to keep in affect. If you start throwing any: [{}] into the mix, you are telling Mongoose that any old object will do for that list. This is, in my opinion, very dangerous to do because if you have two types of objects in your list:

var schema1 = new Schema({
   name:    String,
   age:  { type: Number, min: 18, max: 65 },
   living:  Boolean
})

var schema2 = new Schema({
   name:    String,
   age:  { type: Number, min: 18, max: 65 },
   breed:  Boolean
})

and you were working with a list that combined these two objects, if you tried to grab say breed from the schema1 type object, you would either get an error or an odd value that could break your code. So Mongoose strictly types the objects you are pushing to a list, unless you use something like Mixed or any . Here's a link to the documentation that may help explain a bit as well

https://mongoosejs.com/docs/schematypes.html#arrays

If this doesn't make sense or I explained it poorly or answered the wrong question please comment and I'll fix it to explain as best I can.

Happy coding :-)

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