I'm using Mongo for the first time and I'm having difficulties with creating a document which has an array. The object is passed to a nodejs server like so:
{
"customerid":"121212",
"name": "Zero 2679",
"email": "[email protected]",
"address": "bla bla bla",
"tokens":[{"ctoken":"123456"},{"ctoken":"1234567"}]
}
The code executes the following:
var newCustomer = new Customer(
{
_id: request.body.customerid,
name: request.body.name,
email: request.body.email,
address: request.body.address,
tokens:request.body.tokens
});
newCustomer.save(function (err) {
if (err) winston.log('error', err);
// saved!
})
The schema is configured like so:
var customerSchema = new Schema({
// index: true => Tells mongo to index this paramater because it is used frequently. It makes querying faster
_id: {type: String, unique: true}, // unique customer ID
name: {type: String, default: '', required: true}, // Customer name
//email: {type: mongoose.SchemaTypes.Email, required: true},
email: {type: String, required: true},
address: { type: String, required: true },
toknes: [{ ctoken :{type: String} }]
}, {collection: 'customers'});
When I go and look at the object in the MongoDB, it looks like this:
{
"_id": "121212",
"email": "[email protected]",
"address": "bla bla bla",
"tokens": [],
"name": "Zero 2679",
"__v": 0
}
The tokens are missing and there's a '_v' value which I don't even have.
Please advise what am I doing wrong?