0

I am creating dynamic collection in my application but I am not able to fetch, insert and update that dynamic collection.

Dynamic collection created when I am inserting a Record in different collection. For example I have person collection and want to create dynamic collection of task to dynamically separate task data of every person.

Please help me out how can I implement the same in meteor?

var collectionCache = {};
function create_collection(name) {
    collectionCache[name] = new Mongo.Collection(name);

    Meteor.publish(collectionCache[name], function() {
        return global[collectionCache[name]].find();
    });
}

My collection name is task_parentID.

Now data should saved in task_parentID where parentID is unique ID of that parent.

ekad
  • 14,436
  • 26
  • 44
  • 46
sakshi
  • 171
  • 1
  • 1
  • 14

1 Answers1

0

Just to confirm the problem- You want a document to be created in Task collection when a collection to be created for that Person?

I will advice against thie approach and will recommend you follow this

To explain: Create two collections - Tasks and Persons.

The document inserted in Persons will have a unique id. The Task can have a structure like this:

{ _id : //unique ID
//other fields
assignedTo: //_id of the Person to whom the task is assigned
}

The assignedTo will contain the _id of document in the Persons collection.

When you need to sort, just call the subscription as Tasks.find({assignedTo: _persons_Id}); and you will get all the tasks under the said person.

Community
  • 1
  • 1
Ankit
  • 1,118
  • 13
  • 21
  • No I want to create a new "task_persionId" collection dynamically against new Person document. when I am adding task of that new person, tasks data of that person should go in that collection task_persionId. – sakshi Sep 20 '16 at 12:11
  • Did you check the link I mentioned? It has advised against creating collections on the fly as '`meteor` is not designed for it. The more efficient approach will be to create a single collection and assign a differentiating id as I have have mentioned and in the link too. – Ankit Sep 20 '16 at 12:12
  • Yes I already go through that link . Actually I want to handle client data in separate collection due to some security issue, so I require this feature in Meteor – sakshi Sep 20 '16 at 12:19
  • There you go then: https://stackoverflow.com/questions/15214667/creating-new-meteor-collections-on-the-fly – Ankit Sep 20 '16 at 12:24
  • Thank You so much for your response. Yes I already go through that link, With the help of this I am able to create collection dynamic but not able to insert and fetch data from that collection because collection name is dynamic. I added meteor dev extension to check mini mongo database included the same dynamic collection in it but I have found it is not included. – sakshi Sep 20 '16 at 12:33
  • function create_collection(name) { collectionCache[name] = new Mongo.Collection(name); Meteor.publish(collectionCache[name], function() { return global[collectionCache[name]].find(); }); } – sakshi Sep 20 '16 at 12:45
  • my collection name is task_parentID – sakshi Sep 20 '16 at 12:47
  • now data should saved in task_parentID where parentID is unique ID of that parent – sakshi Sep 20 '16 at 12:47
  • You should probably comment on the answer in the link. Those guys will be able to assist better. – Ankit Sep 20 '16 at 13:00
  • Were you able to solve it? I got some pointer in this directions. Just to diagnose the problem, You said, you were able to create the collections, but were not able to insert? Were you able to insert/read after restarting the server? – Ankit Sep 21 '16 at 05:28