meteor - Clone MongoDB objects in Meteorjs by changing value of a single field -


i working on meteorjs application using mongodb in end. in collection there objects having 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 copy these objects in database changing 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 objects in database

{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 have find elements db have parent_id:'abc' items=db.collection.find({parent_id:'abc').fetch() , using loop have changed parent_id of each item , tried command

for(i=0;i<items.length;i++){     items[i].parent_id='abcdef';     meteor.collection.insert(item) } 

but giving me errorduplicate _id

share|improve question
up vote 2 down vote accepted

well unless delete _id value object first:

for(i=0;i<items.length;i++){     items[i].parent_id='abcdef';     delete items[i]["_id"];     meteor.collection.insert(item[i]) } 

so delete should clear , new _id generated.

share|improve answer
    
yes, works, thank you – sajid ahmad mar 24 '14 @ 10:20

when collection.find() documents can use field specifier exclude _id field.

var items = collection.find({}, {fields: {name: 1, parent_id: 1, _id: 0}).fetch(); 

then when modify , insert documents again, duplicates each having own unique _id.

share|improve answer

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments