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

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"})

Related

update() function disregards limit() in mongo [duplicate]

This question already has answers here:
How to limit number of updating documents in mongodb
(8 answers)
Closed 5 years ago.
Lets say I have a 10 documents of Item in the database.
Lets retrieve 3 documents of Item matching some condition using limit().
documents = Item.objects(somefield=somecondition).limit(3)
Now if I do
documents.update(), mongoengine updates all the documents in the database matched by the query not just the 3 documents I have limited my query to.
I also tried setting multi=False in the params, but then only one document gets updated.
Is there anyway to do update while querying itself instead of looping over the documents one by one?
As far as I know there is no available solution to your problem provided by MongoDB. However you could try something like this
documents.forEach(
function (e) {
e.field = 'value';
db.collection.save(e);
}
);

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

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;
}})

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

mongodb $in limit [duplicate]

This question already has answers here:
What is the maximum number of parameters passed to $in query in MongoDB?
(4 answers)
Closed 6 years ago.
Was just wondering if there is a limit to Mongodb's $in function?
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
I have a collection of users (BIG) and have a smaller subset of ObjectIds stashed somewhere, and I want to select all users (collections) that are in my ObjectIds.
Thanks
Since there's no limit on the number of items in an array as such, you shouldn't have any problem..
For the case when the array is embedded inside a document, you might want to have a look at these:
http://groups.google.com/group/mongodb-user/browse_thread/thread/4a7caeba972aa998?fwc=1
Filtering content based on words
http://groups.google.com/group/mongodb-user/browse_thread/thread/28ae76e5ad5fcfb5