I am working on a Meteorjs application which is using MongoDB in back end.
In my collection there are some objects which are having a common field named parent_id
eg
{name:'A',parent_id:'acd'}
{name:'b',parent_id:'acd'}
{name:'c',parent_id:'acd'}
{name:'d',parent_id:'acd'}
I want to copy all these objects in the database by changing the parent_id
field eg
{name:'A',parent_id:'acdef'}
{name:'b',parent_id:'acdef'}
{name:'c',parent_id:'acdef'}
{name:'d',parent_id:'acdef'}
and these all objects will be in database like this
{name:'A',parent_id:'acd'}
{name:'b',parent_id:'acd'}
{name:'c',parent_id:'acd'}
{name:'d',parent_id:'acd'}
{name:'A',parent_id:'acdef'}
{name:'b',parent_id:'acdef'}
{name:'c',parent_id:'acdef'}
{name:'d',parent_id:'acdef'}
for this I have find the elements from the db which have parent_id:'abc'
items=db.collection.find({parent_id:'abc').fetch()
and using a loop i have changed the parent_id of each item and then tried this command
for(i=0;i<items.length;i++){
items[i].parent_id='abcdef';
meteor.collection.insert(item)
}
but it is giving me an errorduplicate for _id