0

Trying to get unique userNames for a specified time stamp (ts field) range.

db.auditLog.distinct( {"data.userName"}, {ts: /.*05/19/2016*./ )

from

_id: ObjectId("51d33bc8e4b09d71ea4c45d4")
ts: 07/02/2013 04:44:56 PM (-0400)
data:
    userName: "adminuser"

This ain't it but you probably know what I am trying to do.

I would also like to use $gte and $lt if possible.

2 Answers 2

3

Your syntax is almost right. The first parameter to distinct() takes a field name (string), though, not a document.

db.auditLog.distinct( "data.userName", {ts: /.*05/19/2016*./} )

This is based on the assumption that your ts field is a string. Otherwise querying it with a regex wouldn't make sense...

Sign up to request clarification or add additional context in comments.

1 Comment

I did try that in the form of db.auditLog.distinct( "data.userName", {ts: /.*2016*./} ) and even tried 2013 , 2017, etc which I know are there but its returning [ ] . Even /.*19*./ doesn't return anything. using distinct with only data.userName returns several userNames. Perhaps ts is not a string?
0

Ok, ts was a date, sorry for the lack of clarification and thanks for getting me closer to where I could work it out.

db.auditLog.distinct( "data.userName", {ts: { $gte : new ISODate("2017-01-01") }} )

this works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.