-2

I have a document with below fields inside a mongo collection.

{
  _id: policyId_YYYYMMDDHH24MISS,
  createDate: ISO DATE,
  createId: VARCHAR
}

how can i append timestamp 'YYYYMMDDHH24MISS' to a field?

Expected:

{
 _id: CERT00501_20160210132745,
 createDate: ISO DATE,
 createId: abcd1234
}
sandy
  • 1
  • 1
  • 6
  • Do you mean a Unix timestamp? 'YYYYMMDDHH24MISS' is not a timestamp but a custom formatted date, timestamp is the number of seconds elapsed since 1970. – Jonathan Muller Mar 07 '16 at 17:23

1 Answers1

0

Well, you really don't need to store timestamp in MongoDB as it's default _id field do this for you.

However, if you have special needs, you can store timestamp as number in MongoDB.

// in javascript    
var id = "CERT00501_"+ Date.now();

var doc = {
  _id: id,
  createDate: "2012-12-19T06:01:17.171Z",
  createId: ""
}

You'll get a document as below:

{ 
   _id: 'CERT00501_1457373601773',
   createDate: '2012-12-19T06:01:17.171Z',
   createId: '' 
}

Please see more documentation about ObjectId.

Saleem
  • 8,728
  • 2
  • 20
  • 34
  • Saleem I know this question is really old, but is there a way to do something like "db.collection.list()". Let's say the result is "Name: something". I would like to return 2 atributes, name and datenow. is this possible? I can easily do this with sql server but javascript is a pain in the ... – Racer SQL Aug 05 '19 at 19:08