I have a lists
collection where each document has an array of members
. Each element of the members
array is a document with an email
property, creation date
property, and some other meta. I have a unique index on members.email
to prevent the same email being entered into the same list twice, but I would like to retain the original date
value. Unfortunately, neither $addToSet
nor $push
seem to do this.
Example using $push:
$lists->update(array('_id' => $list['_id'], 'members.email' => array('$ne' => $email)), array('$push' => array('members' => array(
'email' => $email,
'date' => new MongoDate(),
// etc.
))));
And with $addToSet:
$lists->update(array('_id' => $list['_id']), array('$addToSet' => array('members' => array(
'email' => $email,
'date' => new MongoDate(),
// etc.
))));
Both examples replace the entire embedded document with the new one due to (I assume) the unique date
value. Is it possible to only $push
the "member" document if members.email
doesn't already exist or will I need to do this in two commands?
Alternatively, would it be better scalability-wise to put the members
in their own collection with a parent_list
-like property?