0

I'm developing a MEAN stack application that needs to insert a list of contacts into a single record in MongoDb.

I'm using the Node-Restful module, which is a great helper.

I'm having trouble figuring out how I would insert/embed a list of contacts into an single user's record. The alternative for me is to do a reference to another collection, but from what I've seen in Mongo, the recommendation would be to embed each contact as a subdocument within a single document.

Has anyone had experience using the Node-Restful module to do this?

https://github.com/baugarten/node-restful

2
  • I suppose you use mongoose, right ? Commented Jul 28, 2016 at 19:04
  • So, yes, you're right, it's better to use subdocuments whenever you can (if the nested array is not too big). For that, you have several options that are RESTful compilant. The simplest one is to send your entire document inside POST/PUT request. Commented Jul 28, 2016 at 20:29

1 Answer 1

1

You can post a object with the related sub-docs. Eg:

{ "name": "Luke Skywalker",
  "films": [
    {
        "director": "George Lucas", 
        "title": "A New Hope"   
    },
    {
        "director": "George Lucas", 
        "title": "Attack of the Clones" 
    }
  ]
}

On my person schema I have a reference to film schema in "films" attribute. By this way you will create the document with the subdocuments in just one post.

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.