0

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?

1
  • 1
    _v is from mongo, don't worry about that Commented Feb 23, 2017 at 19:55

2 Answers 2

2

in your schema

tokens: [{ ctoken : String }]

request.body.tokens must be something like = {ctoken: 'some string'}

       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!
        })

and if the customer db documents contains data, then you should perform push operation

Sign up to request clarification or add additional context in comments.

4 Comments

Tried this, still does not work. Please take a look at my updated question.
in schema there's a toknes field instead of tokens, do a console.log(req.body.tokens)
4 , a typo, I can't believe it.
@p0k8_13, yes it is.
0

In your schema it should be:

tokens: [{ type: String }]

As ctoken is not a data type. The working schema is illustrated below:

var customerSchema = new Schema({
 _id: {type: String, unique: true}, 
 name: {type: String, default: '', required: true},
 email: {type: String, required: true},
 address: { type: String, required: true },
 tokens: [{ type: String }]
}, {collection: 'customers'});

1 Comment

Tried this, still does not work. Please take a look at my updated question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.