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

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.

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 can I apply a filter in MongoDB find operation to check empty string? [duplicate]

This question already has answers here:
Test empty string in mongodb and pymongo
(4 answers)
Closed 5 years ago.
I am working on a application built in PHP using MongoDB as a database.
Data is organized across BSON documents into a collection in MongoDB.
I need to retrieve only those documents where field containing string value is non empty value. I searched for functions equivalent to empty and strlen functions belonging to PHP language but did not get any relevant search results.
try this,
$cursor = $collection->find(array("someField" => array('$ne' => null)));

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: 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 $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