{"timestamp":"$magicTimeExpression"}
Hi,
is it possible to create a Value in a document with a client, which is replaced by the database server with the creation time?
I believe you’re looking for the $currentDate
operator.
Please note that $currentDate
is an update operator. You can use the upsert option if you need to insert a new document instead.
Best regards,
Kevin
Hi javantamt
Which key/value combination should I put into the find-options to be sure that this does not fit any existing document?
This would depend on your schema design and your data. The most straightforward method is to use the _id
field as the search key for the update query, since MongoDB requires the _id
field to be unique, and also creates an index on it automatically. If you have a unique identifier of some kind that exists for all your documents (e.g. UUID or some other identifier specific to your data), then you may be able to use that identifier as the _id
field.
For example, if you have a unique key of 1234
, the command:
db.collection.update({_id:1234}, {$currentDate: {...}, $set: {...}}, {upsert: true})
will create a new document with the specified _id
field if a document with that _id
doesn’t exist.
Best regards,
Kevin