TheCatCanCanACan
TheCatCanCanACan

Reputation: 23

mongodb substr string from behind and compare

I want to use mongodb client query the data like this:

{ "_id" : "1231220140713" }
{ "_id" : "12320140714" },
{ "_id" : "1320140714" },
{ "_id" : "115320140714" }

get the result the data gte 20140714:

{ "_id" : "12320140714" },
{ "_id" : "1320140714" },
{ "_id" : "115320140714" }

the $_id last eight character strings is date type

how can I use db.xxx.find() or db.xxx.aggregate() deal with

Upvotes: 1

Views: 1672

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50426

The $substr of aggregate does not read backwards from the string:

If is a negative number, $substr returns a substring that starts at the specified index and includes the rest of the string.

So that leaves JavaScript evaluation with where:

db.collection.find(function() { return this._id.substr(-8) >= "20140714" })

Not the best, but there presently is not a native operator condition that would return from the end of the string.

Upvotes: 1

Related Questions