user2568374
user2568374

Reputation: 1306

Mongo Query with distinct + Like

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.

Upvotes: 0

Views: 281

Answers (2)

user2568374
user2568374

Reputation: 1306

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.

Upvotes: 0

dnickless
dnickless

Reputation: 10918

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...

Upvotes: 3

Related Questions