Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert current date in document #95

Closed
singhvikram704 opened this issue Jan 15, 2016 · 5 comments
Closed

How to insert current date in document #95

singhvikram704 opened this issue Jan 15, 2016 · 5 comments
Assignees
Milestone

Comments

@singhvikram704
Copy link

How to insert current date in document as like $currentdate in mongo

@mkjsix
Copy link
Member

mkjsix commented Jan 15, 2016

Please check if this answers your question: #89

@mkjsix mkjsix assigned mkjsix and unassigned ujibang Jan 15, 2016
@singhvikram704
Copy link
Author

In this answer we passing date from client. I want to insert mongo server's current date as do at shell with $currentDate.

@ujibang
Copy link
Contributor

ujibang commented Jan 16, 2016

Hi @singhvikram704

if you want restheart to add a time stamp you can rely on Representation Tranformers

There is a ready to use transformer called addRequestProperties that allows you to inject some useful properties to write requests. Specifically you can add the dateTime property that is the request date.

Note that this solution is much more reliable if you want to make sure that the client doesn't alter somehow the timestamp. In fact using $currentDate is still controlled by the client who could send whatever value.

You can read the documentation for more details, in summary you add the rts metadata property to the collection specifying the addRequestProperties transformer with scope "CHILDREN" and phase "REQUEST" and restheart will use it for the write requests on collection documents.

PATCH /db/coll
{ rts: [
    {
     "name": "addRequestProperties", 
     "phase": "REQUEST", 
     "scope": "CHILDREN",
     "args": { "log": [ "dateTime", "epochTimeStamp" ] }
   }
] }

Now if you add a document to /db/coll you'll see the property being injected.

PUT /db/coll/doc { "a": 1 }
GET /db/coll/doc

HTTP/1.1 200 OK
...

{
    "_id": "doc", 
    "a": "1", 
    "log": {
        "dateTime": "[16/Jan/2016:09:47:55 +0100]", 
        "epochTimeStamp": 1452934075
    },
    ...
}

Note that dateTime and epochTimeStamp are not of type $date though. You can easily create a custom transformer to fit your needs.

@ujibang
Copy link
Contributor

ujibang commented Jan 16, 2016

regarding support for $currentDate and other field update operators I have created task https://softinstigate.atlassian.net/browse/RH-151

current master branch already contains a version that supports them on PATCH /db/coll/doc requests

@ujibang
Copy link
Contributor

ujibang commented Jan 16, 2016

@singhvikram704

thanks for pointing out this. I added support for update operators to all resources/verbs.

this improvement will be available in next release 1.2

Basically now you can do:

PUT /db/coll/doc { a: 1, { "$currentDate": {"a": true}}, { "$push": {"array": "item"} } }
PATCH /db/coll/doc { b:2, { "$push": {"array": "item"} } }
GET /db/coll/doc
HTTP/1.1 200 OK
...

{
    "_etag": {
        "$oid": "569a62932d174c7ccf07c7e4"
    }, 
    "_id": "doc", 
    "a": {
        "$date": 1452958349130
    }, 
    "array": [
        "item", 
        "item"
    ],
   ...
}

@mkjsix mkjsix assigned ujibang and unassigned mkjsix Jan 16, 2016
@mkjsix mkjsix added the news label Jan 16, 2016
@mkjsix mkjsix added this to the 1.2.0 milestone Jan 16, 2016
@ujibang ujibang closed this as completed Jan 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants