0

I've been playing around with casperjs to login to a site and grab some data that I need. Currently I have a json object that outputs data like this but with much more fields.

{
    "Activity #": "1-1IMHEEJM",
    "Customer": "DOE, JOHN",
    "Tenure": "0 Year 0 Month",
    "Account #": "12345678",
    "Phone #": "(111) 222-3333",
    "Alt Phone #": "",
    "Agent Name": "Michelle"
}

and my mongoose schema looks like this.

var workOrderSchema = new mongoose.Schema({
  activityNumber: {type: String},
  customer: {type: String},
  tenure: {type: String},
  accountNumber: {type: Number},
  phoneNumber: {type: String},
  altPhoneNumber: {type: String},
  agentName: {type: String}
});

How can I get my data in mongodb using my schema? I can insert the object right into mongo but my fields won't match my schema i.e "Activity #": not activityNumber:

I hope my question is clear and someone can put me in the right direction.

Thanks.

2
  • You can just change the field names to match those of the schema, or update the schema to match those field names Commented Dec 28, 2013 at 21:14
  • I actually want to match the schema. My problem is that I'm getting the field names automatically. since I already have a schema I didn't want to start renaming a bunch a fields. Commented Dec 28, 2013 at 21:44

1 Answer 1

1

Just map/rename the fields as you insert the object using Mongoose.

Assuming obj is your object to insert and WorkOrder is your model:

WorkOrder.create({
    activityNumber: obj["Activity #"],
    customer: obj["Customer"],
    tenure: obj["Tenure"],
    accountNumber: obj["Account #"],
    phoneNumber: obj["Phone #"],
    altPhoneNumber: obj["Alt Phone #"],
    agentName: obj["Agent Name"]
}, callback);
Sign up to request clarification or add additional context in comments.

Comments

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.