Add one or more records to a collection in your database.
This adds record(s) to the join table that is automatically generated for a collection association (i.e. Many-to-Many). It accepts either the primary key of the record (string or number) or the data for a new record (dictionary/object). If a dictionary is specified, it will be used to create a new record and automatically associate it as a member of this collection association.
.add(recordsToAdd);
// Don't forget to call `.save()` afterwards!
Argument | Type | Details | |
---|---|---|---|
1 | recordsToAdd | The primary key of the record to add, an array of primary keys of records to add, or a dictionary of data representing a new record to create and then add. |
To help a user named Finn in the database adopt a pet named Jake:
User.findOne({name:'Finn'}).populate('adoptedPets').exec(function (err, finn){
if (err) { return res.serverError(err); }
if (!finn) { return res.notFound('Could not find a user named Finn.'); }
Pet.findOne({name:'Jake'}).exec(function (err, jake){
if (err) { return res.serverError(err); }
if (!jake) { return res.notFound('Could not find a pet named Jake.'); }
finn.adoptedPets.add(jake.id);
finn.save(function(err){
if (err) { return res.serverError(err); }
return res.ok();
});//</save()>
});//</Pet.findOne()>
});//</User.findOne()>
.add()
alone won't actually persist the change in associations to the database. You should always call.save()
after you finish using.add()
and/or.remove()
.- Attempting to add an association that already exists will throw an error. See here for an example.