How to compare 2 ISODate fields in Mongo? [duplicate] - mongodb

This question already has answers here:
How to compare two strings in mongoDB spring data?
(1 answer)
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 5 years ago.
I have 2 fields like this in my document
"DATE_1" : ISODate("2017-08-11T04:00:00Z")
"DATE_2" : ISODate("2017-06-12T04:00:00Z")
I would like to select documents with "DATE_1" is greater than "DATE_2" and i tried the following query which should return the above document.But its not giving any result
db.collection.find({"DATE_1":{$gte:"DATE_2"}})
How do I compare 2 ISODate fields in Mongo? Also how do I make this query using Spring date mongodb? something like this?
Criteria.where("DATE_1").gte("DATE_2");

This should work. Can you please try this and give your feedback.
db.getCollection('collectionName').find({$where: function() {
return this.DATE_1 > this.DATE_2;
}})

Related

Golang MongoDB Driver return ordered field [duplicate]

This question already has answers here:
Using MongoDB Projection
(1 answer)
How to filter fields from a mongo document with the official mongo-go-driver
(1 answer)
Closed 8 months ago.
There are lots example about how to use go mongodb driver, but less or no example about return ordered field. For example, my document has lots of feilds, but I only want my mongo query return some of fields. Can anyone help me?
https://github.com/mongodb/mongo-go-driver

How to display records in descending order in mongodb [duplicate]

This question already has answers here:
How to do alphanumeric sort in mongoDB?
(2 answers)
Closed 4 years ago.
I have a table in MongoDB in which I can get the fields in descending order
sorted by system_id
db.job_parameters_mongo.find().sort({system_id : -1})
This is fine but gives me incorrect result as system_id is string field.Is there any way that I can convert system_id to number before ordering in descending order.I saw on this site that there is a way to do this using forEach (how to convert string to numerical values in mongodb) but that uses a function.Isn't there any other way like we have to_number in rdbms?
You can use $toInt available in 4.0 version.
db.job_parameters_mongo.aggregate([
{"$addFields":{"system_id":{"$toInt":"$system_id"}}},
{"$sort":{"system_id":-1}}
])

MongoDB: Query documents with a field greather than another [duplicate]

This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 6 years ago.
Is it possible in mongoDB to query documents where a field's value is greather than another one of the same document ?
I want to query each document where a > b for documents as:
{
a: Number,
b: Number,
}
Is it possible to reference a field's value in query to another one ?
Yes it is possible. Consider that you are trying to get the documents where a is greater than b. The code below is doing that.
db.collectionName.find({$where: "this.a > this.b"})

Mongodb - Finding a field value existence in another field [duplicate]

This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 8 years ago.
Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks
Categories.find({'_id': 'parentID'})
{
_id: 11,
parentID: 1
}
I am using MongoDB 2.6.7
Yes, this is easy to do with the $where operator.
db.Categories.find({'$where':"this._id === this.parent"})
This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.

check if one key is greater than another key in mongodb [duplicate]

This question already has answers here:
MongoDB : aggregation framework : $match between fields
(2 answers)
Closed 8 years ago.
I want to compare one key value with another key value in mongodb. can any one help me with this query.
example:
{
"_id":"xxxxx",
"game":"xxx",
"score":100,
"hi-score":200
}
i want to check if score is greater than hi-score
Try the below query using http://docs.mongodb.org/manual/reference/operator/query/where/
db.collection.find( { $where: "this.score > this.hi-score" } );
Also, please read the warning section provided in the link.
PS: I have not tried this